routerparse.c
上传用户:awang829
上传日期:2019-07-14
资源大小:2356k
文件大小:134k
- /* Copyright (c) 2001 Matej Pfajfar.
- * Copyright (c) 2001-2004, Roger Dingledine.
- * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2009, The Tor Project, Inc. */
- /* See LICENSE for licensing information */
- /**
- * file routerparse.c
- * brief Code to parse and validate router descriptors and directories.
- **/
- #include "or.h"
- #include "memarea.h"
- /****************************************************************************/
- /** Enumeration of possible token types. The ones starting with K_ correspond
- * to directory 'keywords'. _ERR is an error in the tokenizing process, _EOF
- * is an end-of-file marker, and _NIL is used to encode not-a-token.
- */
- typedef enum {
- K_ACCEPT = 0,
- K_ACCEPT6,
- K_DIRECTORY_SIGNATURE,
- K_RECOMMENDED_SOFTWARE,
- K_REJECT,
- K_REJECT6,
- K_ROUTER,
- K_SIGNED_DIRECTORY,
- K_SIGNING_KEY,
- K_ONION_KEY,
- K_ROUTER_SIGNATURE,
- K_PUBLISHED,
- K_RUNNING_ROUTERS,
- K_ROUTER_STATUS,
- K_PLATFORM,
- K_OPT,
- K_BANDWIDTH,
- K_CONTACT,
- K_NETWORK_STATUS,
- K_UPTIME,
- K_DIR_SIGNING_KEY,
- K_FAMILY,
- K_FINGERPRINT,
- K_HIBERNATING,
- K_READ_HISTORY,
- K_WRITE_HISTORY,
- K_NETWORK_STATUS_VERSION,
- K_DIR_SOURCE,
- K_DIR_OPTIONS,
- K_CLIENT_VERSIONS,
- K_SERVER_VERSIONS,
- K_P,
- K_R,
- K_S,
- K_V,
- K_W,
- K_EVENTDNS,
- K_EXTRA_INFO,
- K_EXTRA_INFO_DIGEST,
- K_CACHES_EXTRA_INFO,
- K_HIDDEN_SERVICE_DIR,
- K_ALLOW_SINGLE_HOP_EXITS,
- K_DIR_KEY_CERTIFICATE_VERSION,
- K_DIR_IDENTITY_KEY,
- K_DIR_KEY_PUBLISHED,
- K_DIR_KEY_EXPIRES,
- K_DIR_KEY_CERTIFICATION,
- K_DIR_KEY_CROSSCERT,
- K_DIR_ADDRESS,
- K_VOTE_STATUS,
- K_VALID_AFTER,
- K_FRESH_UNTIL,
- K_VALID_UNTIL,
- K_VOTING_DELAY,
- K_KNOWN_FLAGS,
- K_PARAMS,
- K_VOTE_DIGEST,
- K_CONSENSUS_DIGEST,
- K_CONSENSUS_METHODS,
- K_CONSENSUS_METHOD,
- K_LEGACY_DIR_KEY,
- A_PURPOSE,
- _A_UNKNOWN,
- R_RENDEZVOUS_SERVICE_DESCRIPTOR,
- R_VERSION,
- R_PERMANENT_KEY,
- R_SECRET_ID_PART,
- R_PUBLICATION_TIME,
- R_PROTOCOL_VERSIONS,
- R_INTRODUCTION_POINTS,
- R_SIGNATURE,
- R_IPO_IDENTIFIER,
- R_IPO_IP_ADDRESS,
- R_IPO_ONION_PORT,
- R_IPO_ONION_KEY,
- R_IPO_SERVICE_KEY,
- C_CLIENT_NAME,
- C_DESCRIPTOR_COOKIE,
- C_CLIENT_KEY,
- _ERR,
- _EOF,
- _NIL
- } directory_keyword;
- #define MIN_ANNOTATION A_PURPOSE
- #define MAX_ANNOTATION _A_UNKNOWN
- /** Structure to hold a single directory token.
- *
- * We parse a directory by breaking it into "tokens", each consisting
- * of a keyword, a line full of arguments, and a binary object. The
- * arguments and object are both optional, depending on the keyword
- * type.
- *
- * This structure is only allocated in memareas; do not allocate it on
- * the heap, or token_free() won't work.
- */
- typedef struct directory_token_t {
- directory_keyword tp; /**< Type of the token. */
- int n_args:30; /**< Number of elements in args */
- char **args; /**< Array of arguments from keyword line. */
- char *object_type; /**< -----BEGIN [object_type]-----*/
- size_t object_size; /**< Bytes in object_body */
- char *object_body; /**< Contents of object, base64-decoded. */
- crypto_pk_env_t *key; /**< For public keys only. Heap-allocated. */
- char *error; /**< For _ERR tokens only. */
- } directory_token_t;
- /* ********************************************************************** */
- /** We use a table of rules to decide how to parse each token type. */
- /** Rules for whether the keyword needs an object. */
- typedef enum {
- NO_OBJ, /**< No object, ever. */
- NEED_OBJ, /**< Object is required. */
- NEED_SKEY_1024,/**< Object is required, and must be a 1024 bit private key */
- NEED_KEY_1024, /**< Object is required, and must be a 1024 bit public key */
- NEED_KEY, /**< Object is required, and must be a public key. */
- OBJ_OK, /**< Object is optional. */
- } obj_syntax;
- #define AT_START 1
- #define AT_END 2
- /** Determines the parsing rules for a single token type. */
- typedef struct token_rule_t {
- /** The string value of the keyword identifying the type of item. */
- const char *t;
- /** The corresponding directory_keyword enum. */
- directory_keyword v;
- /** Minimum number of arguments for this item */
- int min_args;
- /** Maximum number of arguments for this item */
- int max_args;
- /** If true, we concatenate all arguments for this item into a single
- * string. */
- int concat_args;
- /** Requirements on object syntax for this item. */
- obj_syntax os;
- /** Lowest number of times this item may appear in a document. */
- int min_cnt;
- /** Highest number of times this item may appear in a document. */
- int max_cnt;
- /** One or more of AT_START/AT_END to limit where the item may appear in a
- * document. */
- int pos;
- /** True iff this token is an annotation. */
- int is_annotation;
- } token_rule_t;
- /*
- * Helper macros to define token tables. 's' is a string, 't' is a
- * directory_keyword, 'a' is a trio of argument multiplicities, and 'o' is an
- * object syntax.
- *
- */
- /** Appears to indicate the end of a table. */
- #define END_OF_TABLE { NULL, _NIL, 0,0,0, NO_OBJ, 0, INT_MAX, 0, 0 }
- /** An item with no restrictions: used for obsolete document types */
- #define T(s,t,a,o) { s, t, a, o, 0, INT_MAX, 0, 0 }
- /** An item with no restrictions on multiplicity or location. */
- #define T0N(s,t,a,o) { s, t, a, o, 0, INT_MAX, 0, 0 }
- /** An item that must appear exactly once */
- #define T1(s,t,a,o) { s, t, a, o, 1, 1, 0, 0 }
- /** An item that must appear exactly once, at the start of the document */
- #define T1_START(s,t,a,o) { s, t, a, o, 1, 1, AT_START, 0 }
- /** An item that must appear exactly once, at the end of the document */
- #define T1_END(s,t,a,o) { s, t, a, o, 1, 1, AT_END, 0 }
- /** An item that must appear one or more times */
- #define T1N(s,t,a,o) { s, t, a, o, 1, INT_MAX, 0, 0 }
- /** An item that must appear no more than once */
- #define T01(s,t,a,o) { s, t, a, o, 0, 1, 0, 0 }
- /** An annotation that must appear no more than once */
- #define A01(s,t,a,o) { s, t, a, o, 0, 1, 0, 1 }
- /* Argument multiplicity: any number of arguments. */
- #define ARGS 0,INT_MAX,0
- /* Argument multiplicity: no arguments. */
- #define NO_ARGS 0,0,0
- /* Argument multiplicity: concatenate all arguments. */
- #define CONCAT_ARGS 1,1,1
- /* Argument multiplicity: at least <b>n</b> arguments. */
- #define GE(n) n,INT_MAX,0
- /* Argument multiplicity: exactly <b>n</b> arguments. */
- #define EQ(n) n,n,0
- /** List of tokens allowable in router descriptors */
- static token_rule_t routerdesc_token_table[] = {
- T0N("reject", K_REJECT, ARGS, NO_OBJ ),
- T0N("accept", K_ACCEPT, ARGS, NO_OBJ ),
- T0N("reject6", K_REJECT6, ARGS, NO_OBJ ),
- T0N("accept6", K_ACCEPT6, ARGS, NO_OBJ ),
- T1_START( "router", K_ROUTER, GE(5), NO_OBJ ),
- T1( "signing-key", K_SIGNING_KEY, NO_ARGS, NEED_KEY_1024 ),
- T1( "onion-key", K_ONION_KEY, NO_ARGS, NEED_KEY_1024 ),
- T1_END( "router-signature", K_ROUTER_SIGNATURE, NO_ARGS, NEED_OBJ ),
- T1( "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
- T01("uptime", K_UPTIME, GE(1), NO_OBJ ),
- T01("fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
- T01("hibernating", K_HIBERNATING, GE(1), NO_OBJ ),
- T01("platform", K_PLATFORM, CONCAT_ARGS, NO_OBJ ),
- T01("contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
- T01("read-history", K_READ_HISTORY, ARGS, NO_OBJ ),
- T01("write-history", K_WRITE_HISTORY, ARGS, NO_OBJ ),
- T01("extra-info-digest", K_EXTRA_INFO_DIGEST, GE(1), NO_OBJ ),
- T01("hidden-service-dir", K_HIDDEN_SERVICE_DIR, NO_ARGS, NO_OBJ ),
- T01("allow-single-hop-exits",K_ALLOW_SINGLE_HOP_EXITS, NO_ARGS, NO_OBJ ),
- T01("family", K_FAMILY, ARGS, NO_OBJ ),
- T01("caches-extra-info", K_CACHES_EXTRA_INFO, NO_ARGS, NO_OBJ ),
- T01("eventdns", K_EVENTDNS, ARGS, NO_OBJ ),
- T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
- T1( "bandwidth", K_BANDWIDTH, GE(3), NO_OBJ ),
- A01("@purpose", A_PURPOSE, GE(1), NO_OBJ ),
- END_OF_TABLE
- };
- /** List of tokens allowable in extra-info documents. */
- static token_rule_t extrainfo_token_table[] = {
- T1_END( "router-signature", K_ROUTER_SIGNATURE, NO_ARGS, NEED_OBJ ),
- T1( "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
- T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
- T01("read-history", K_READ_HISTORY, ARGS, NO_OBJ ),
- T01("write-history", K_WRITE_HISTORY, ARGS, NO_OBJ ),
- T1_START( "extra-info", K_EXTRA_INFO, GE(2), NO_OBJ ),
- END_OF_TABLE
- };
- /** List of tokens allowable in the body part of v2 and v3 networkstatus
- * documents. */
- static token_rule_t rtrstatus_token_table[] = {
- T01("p", K_P, CONCAT_ARGS, NO_OBJ ),
- T1( "r", K_R, GE(8), 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("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
- END_OF_TABLE
- };
- /** List of tokens allowable in the header part of v2 networkstatus documents.
- */
- static token_rule_t netstatus_token_table[] = {
- T1( "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
- T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
- T1( "contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
- T1( "dir-signing-key", K_DIR_SIGNING_KEY, NO_ARGS, NEED_KEY_1024 ),
- T1( "fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
- T1_START("network-status-version", K_NETWORK_STATUS_VERSION,
- GE(1), NO_OBJ ),
- T1( "dir-source", K_DIR_SOURCE, GE(3), NO_OBJ ),
- T01("dir-options", K_DIR_OPTIONS, ARGS, NO_OBJ ),
- T01("client-versions", K_CLIENT_VERSIONS, CONCAT_ARGS, NO_OBJ ),
- T01("server-versions", K_SERVER_VERSIONS, CONCAT_ARGS, NO_OBJ ),
- END_OF_TABLE
- };
- /** List of tokens allowable in the footer of v1/v2 directory/networkstatus
- * footers. */
- static token_rule_t dir_footer_token_table[] = {
- T1("directory-signature", K_DIRECTORY_SIGNATURE, EQ(1), NEED_OBJ ),
- END_OF_TABLE
- };
- /** List of tokens allowable in v1 directory headers/footers. */
- static token_rule_t dir_token_table[] = {
- /* don't enforce counts; this is obsolete. */
- T( "network-status", K_NETWORK_STATUS, NO_ARGS, NO_OBJ ),
- T( "directory-signature", K_DIRECTORY_SIGNATURE, ARGS, NEED_OBJ ),
- T( "recommended-software",K_RECOMMENDED_SOFTWARE,CONCAT_ARGS, NO_OBJ ),
- T( "signed-directory", K_SIGNED_DIRECTORY, NO_ARGS, NO_OBJ ),
- T( "running-routers", K_RUNNING_ROUTERS, ARGS, NO_OBJ ),
- T( "router-status", K_ROUTER_STATUS, ARGS, NO_OBJ ),
- T( "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
- T( "opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
- T( "contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
- T( "dir-signing-key", K_DIR_SIGNING_KEY, ARGS, OBJ_OK ),
- T( "fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
- 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 ),
- T01("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 allowable 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 allowable 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 allowed 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 allowed 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 allowed in V3 networkstatus votes. */
- static token_rule_t networkstatus_token_table[] = {
- T1("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 ),
- CERTIFICATE_MEMBERS
- 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 allowed in V3 networkstatus consensuses. */
- static token_rule_t networkstatus_consensus_token_table[] = {
- T1("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 ),
- END_OF_TABLE
- };
- /** List of tokens allowable in the footer of v1/v2 directory/networkstatus
- * footers. */
- static token_rule_t networkstatus_vote_footer_token_table[] = {
- T( "directory-signature", K_DIRECTORY_SIGNATURE, GE(2), NEED_OBJ ),
- END_OF_TABLE
- };
- /** List of tokens allowable 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 ),
- 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 ),
- T1N("directory-signature", K_DIRECTORY_SIGNATURE, GE(2), NEED_OBJ ),
- END_OF_TABLE
- };
- #undef T
- /* static function prototypes */
- static int router_add_exit_policy(routerinfo_t *router,directory_token_t *tok);
- static addr_policy_t *router_parse_addr_policy(directory_token_t *tok);
- static addr_policy_t *router_parse_addr_policy_private(directory_token_t *tok);
- static int router_get_hash_impl(const char *s, char *digest,
- const char *start_str, const char *end_str,
- char end_char);
- static void token_free(directory_token_t *tok);
- static smartlist_t *find_all_exitpolicy(smartlist_t *s);
- static directory_token_t *_find_by_keyword(smartlist_t *s,
- directory_keyword keyword,
- const char *keyword_str);
- #define find_by_keyword(s, keyword) _find_by_keyword((s), (keyword), #keyword)
- static directory_token_t *find_opt_by_keyword(smartlist_t *s,
- directory_keyword keyword);
- #define TS_ANNOTATIONS_OK 1
- #define TS_NOCHECK 2
- #define TS_NO_NEW_ANNOTATIONS 4
- static int tokenize_string(memarea_t *area,
- const char *start, const char *end,
- smartlist_t *out,
- token_rule_t *table,
- int flags);
- static directory_token_t *get_next_token(memarea_t *area,
- const char **s,
- const char *eos,
- token_rule_t *table);
- #define CST_CHECK_AUTHORITY (1<<0)
- #define CST_NO_CHECK_OBJTYPE (1<<1)
- static int check_signature_token(const char *digest,
- directory_token_t *tok,
- crypto_pk_env_t *pkey,
- int flags,
- const char *doctype);
- static crypto_pk_env_t *find_dir_signing_key(const char *str, const char *eos);
- static int tor_version_same_series(tor_version_t *a, tor_version_t *b);
- #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
- #define DUMP_AREA(a,name) STMT_NIL
- #endif
- /** 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,digest,
- "signed-directory","ndirectory-signature",'n');
- }
- /** 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.
- */
- int
- router_get_router_hash(const char *s, char *digest)
- {
- return router_get_hash_impl(s,digest,
- "router ","nrouter-signature", 'n');
- }
- /** Set <b>digest</b> to the SHA-1 digest of the hash of the running-routers
- * string in <b>s</b>. Return 0 on success, -1 on failure.
- */
- int
- router_get_runningrouters_hash(const char *s, char *digest)
- {
- return router_get_hash_impl(s,digest,
- "network-status","ndirectory-signature", 'n');
- }
- /** Set <b>digest</b> to the SHA-1 digest of the hash of the network-status
- * string in <b>s</b>. Return 0 on success, -1 on failure. */
- int
- router_get_networkstatus_v2_hash(const char *s, char *digest)
- {
- return router_get_hash_impl(s,digest,
- "network-status-version","ndirectory-signature",
- 'n');
- }
- /** Set <b>digest</b> to the SHA-1 digest of the hash of the network-status
- * string in <b>s</b>. Return 0 on success, -1 on failure. */
- int
- router_get_networkstatus_v3_hash(const char *s, char *digest)
- {
- return router_get_hash_impl(s,digest,
- "network-status-version","ndirectory-signature",
- ' ');
- }
- /** Set <b>digest</b> to the SHA-1 digest of the hash of the extrainfo
- * string in <b>s</b>. Return 0 on success, -1 on failure. */
- int
- router_get_extrainfo_hash(const char *s, char *digest)
- {
- return router_get_hash_impl(s,digest,"extra-info","nrouter-signature",'n');
- }
- /** 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 an 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,
- crypto_pk_env_t *private_key)
- {
- char *signature;
- size_t i;
- signature = tor_malloc(crypto_pk_keysize(private_key));
- if (crypto_pk_private_sign(private_key, signature, digest, DIGEST_LEN) < 0) {
- log_warn(LD_BUG,"Couldn't sign digest.");
- goto err;
- }
- if (strlcat(buf, "-----BEGIN SIGNATURE-----n", buf_len) >= buf_len)
- goto truncated;
- i = strlen(buf);
- if (base64_encode(buf+i, buf_len-i, signature, 128) < 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 0;
- truncated:
- log_warn(LD_BUG,"tried to exceed string length.");
- err:
- tor_free(signature);
- return -1;
- }
- /** Return VS_RECOMMENDED if <b>myversion</b> is contained in
- * <b>versionlist</b>. Else, return VS_EMPTY if versionlist has no
- * entries. Else, return VS_OLD if every member of
- * <b>versionlist</b> is newer than <b>myversion</b>. Else, return
- * VS_NEW_IN_SERIES if there is at least one member of <b>versionlist</b> in
- * the same series (major.minor.micro) as <b>myversion</b>, but no such member
- * is newer than <b>myversion.</b>. Else, return VS_NEW if every member of
- * <b>versionlist</b> is older than <b>myversion</b>. Else, return
- * VS_UNRECOMMENDED.
- *
- * (versionlist is a comma-separated list of version strings,
- * optionally prefixed with "Tor". Versions that can't be parsed are
- * ignored.)
- */
- version_status_t
- tor_version_is_obsolete(const char *myversion, const char *versionlist)
- {
- tor_version_t mine, other;
- int found_newer = 0, found_older = 0, found_newer_in_series = 0,
- found_any_in_series = 0, r, same;
- version_status_t ret = VS_UNRECOMMENDED;
- smartlist_t *version_sl;
- log_debug(LD_CONFIG,"Checking whether version '%s' is in '%s'",
- myversion, versionlist);
- if (tor_version_parse(myversion, &mine)) {
- log_err(LD_BUG,"I couldn't parse my own version (%s)", myversion);
- tor_assert(0);
- }
- version_sl = smartlist_create();
- smartlist_split_string(version_sl, versionlist, ",", SPLIT_SKIP_SPACE, 0);
- if (!strlen(versionlist)) { /* no authorities cared or agreed */
- ret = VS_EMPTY;
- goto done;
- }
- SMARTLIST_FOREACH(version_sl, const char *, cp, {
- if (!strcmpstart(cp, "Tor "))
- cp += 4;
- if (tor_version_parse(cp, &other)) {
- /* Couldn't parse other; it can't be a match. */
- } else {
- same = tor_version_same_series(&mine, &other);
- if (same)
- found_any_in_series = 1;
- r = tor_version_compare(&mine, &other);
- if (r==0) {
- ret = VS_RECOMMENDED;
- goto done;
- } else if (r<0) {
- found_newer = 1;
- if (same)
- found_newer_in_series = 1;
- } else if (r>0) {
- found_older = 1;
- }
- }
- });
- /* We didn't find the listed version. Is it new or old? */
- if (found_any_in_series && !found_newer_in_series && found_newer) {
- ret = VS_NEW_IN_SERIES;
- } else if (found_newer && !found_older) {
- ret = VS_OLD;
- } else if (found_older && !found_newer) {
- ret = VS_NEW;
- } else {
- ret = VS_UNRECOMMENDED;
- }
- done:
- SMARTLIST_FOREACH(version_sl, char *, version, tor_free(version));
- smartlist_free(version_sl);
- return ret;
- }
- /** Read a signed directory from <b>str</b>. If it's well-formed, return 0.
- * Otherwise, return -1. If we're a directory cache, cache it.
- */
- int
- router_parse_directory(const char *str)
- {
- directory_token_t *tok;
- char digest[DIGEST_LEN];
- time_t published_on;
- int r;
- const char *end, *cp;
- smartlist_t *tokens = NULL;
- crypto_pk_env_t *declared_key = NULL;
- memarea_t *area = memarea_new();
- /* XXXX This could be simplified a lot, but it will all go away
- * once pre-0.1.1.8 is obsolete, and for now it's better not to
- * touch it. */
- if (router_get_dir_hash(str, digest)) {
- log_warn(LD_DIR, "Unable to compute digest of directory");
- goto err;
- }
- log_debug(LD_DIR,"Received directory hashes to %s",hex_str(digest,4));
- /* Check signature first, before we try to tokenize. */
- cp = str;
- while (cp && (end = strstr(cp+1, "ndirectory-signature")))
- cp = end;
- if (cp == str || !cp) {
- log_warn(LD_DIR, "No signature found on directory."); goto err;
- }
- ++cp;
- tokens = smartlist_create();
- if (tokenize_string(area,cp,strchr(cp,' '),tokens,dir_token_table,0)) {
- log_warn(LD_DIR, "Error tokenizing directory signature"); goto err;
- }
- if (smartlist_len(tokens) != 1) {
- log_warn(LD_DIR, "Unexpected number of tokens in signature"); goto err;
- }
- tok=smartlist_get(tokens,0);
- if (tok->tp != K_DIRECTORY_SIGNATURE) {
- log_warn(LD_DIR,"Expected a single directory signature"); goto err;
- }
- declared_key = find_dir_signing_key(str, str+strlen(str));
- note_crypto_pk_op(VERIFY_DIR);
- if (check_signature_token(digest, tok, declared_key,
- CST_CHECK_AUTHORITY, "directory")<0)
- goto err;
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
- smartlist_clear(tokens);
- memarea_clear(area);
- /* Now try to parse the first part of the directory. */
- if ((end = strstr(str,"nrouter "))) {
- ++end;
- } else if ((end = strstr(str, "ndirectory-signature"))) {
- ++end;
- } else {
- end = str + strlen(str);
- }
- if (tokenize_string(area,str,end,tokens,dir_token_table,0)) {
- log_warn(LD_DIR, "Error tokenizing directory"); goto err;
- }
- tok = find_by_keyword(tokens, K_PUBLISHED);
- tor_assert(tok->n_args == 1);
- if (parse_iso_time(tok->args[0], &published_on) < 0) {
- goto err;
- }
- /* Now that we know the signature is okay, and we have a
- * publication time, cache the directory. */
- if (directory_caches_v1_dir_info(get_options()) &&
- !authdir_mode_v1(get_options()))
- dirserv_set_cached_directory(str, published_on, 0);
- r = 0;
- goto done;
- err:
- r = -1;
- done:
- if (declared_key) crypto_free_pk_env(declared_key);
- if (tokens) {
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
- smartlist_free(tokens);
- }
- if (area) {
- DUMP_AREA(area, "v1 directory");
- memarea_drop_all(area);
- }
- return r;
- }
- /** Read a signed router status statement from <b>str</b>. If it's
- * well-formed, return 0. Otherwise, return -1. If we're a directory cache,
- * cache it.*/
- int
- router_parse_runningrouters(const char *str)
- {
- char digest[DIGEST_LEN];
- directory_token_t *tok;
- time_t published_on;
- int r = -1;
- crypto_pk_env_t *declared_key = NULL;
- smartlist_t *tokens = NULL;
- const char *eos = str + strlen(str);
- memarea_t *area = NULL;
- if (router_get_runningrouters_hash(str, digest)) {
- log_warn(LD_DIR, "Unable to compute digest of running-routers");
- goto err;
- }
- area = memarea_new();
- tokens = smartlist_create();
- if (tokenize_string(area,str,eos,tokens,dir_token_table,0)) {
- log_warn(LD_DIR, "Error tokenizing running-routers"); goto err;
- }
- tok = smartlist_get(tokens,0);
- if (tok->tp != K_NETWORK_STATUS) {
- log_warn(LD_DIR, "Network-status starts with wrong token");
- goto err;
- }
- tok = find_by_keyword(tokens, K_PUBLISHED);
- tor_assert(tok->n_args == 1);
- if (parse_iso_time(tok->args[0], &published_on) < 0) {
- goto err;
- }
- if (!(tok = find_opt_by_keyword(tokens, K_DIRECTORY_SIGNATURE))) {
- log_warn(LD_DIR, "Missing signature on running-routers");
- goto err;
- }
- declared_key = find_dir_signing_key(str, eos);
- note_crypto_pk_op(VERIFY_DIR);
- if (check_signature_token(digest, tok, declared_key,
- CST_CHECK_AUTHORITY, "running-routers")
- < 0)
- goto err;
- /* Now that we know the signature is okay, and we have a
- * publication time, cache the list. */
- if (get_options()->DirPort && !authdir_mode_v1(get_options()))
- dirserv_set_cached_directory(str, published_on, 1);
- r = 0;
- err:
- if (declared_key) crypto_free_pk_env(declared_key);
- if (tokens) {
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
- smartlist_free(tokens);
- }
- if (area) {
- DUMP_AREA(area, "v1 running-routers");
- memarea_drop_all(area);
- }
- return r;
- }
- /** Given a directory or running-routers string in <b>str</b>, try to
- * find the its dir-signing-key token (if any). If this token is
- * present, extract and return the key. Return NULL on failure. */
- static crypto_pk_env_t *
- find_dir_signing_key(const char *str, const char *eos)
- {
- const char *cp;
- directory_token_t *tok;
- crypto_pk_env_t *key = NULL;
- memarea_t *area = NULL;
- tor_assert(str);
- tor_assert(eos);
- /* Is there a dir-signing-key in the directory? */
- cp = tor_memstr(str, eos-str, "nopt dir-signing-key");
- if (!cp)
- cp = tor_memstr(str, eos-str, "ndir-signing-key");
- if (!cp)
- return NULL;
- ++cp; /* Now cp points to the start of the token. */
- area = memarea_new();
- tok = get_next_token(area, &cp, eos, dir_token_table);
- if (!tok) {
- log_warn(LD_DIR, "Unparseable dir-signing-key token");
- goto done;
- }
- if (tok->tp != K_DIR_SIGNING_KEY) {
- log_warn(LD_DIR, "Dir-signing-key token did not parse as expected");
- goto done;
- }
- if (tok->key) {
- key = tok->key;
- tok->key = NULL; /* steal reference. */
- } else {
- log_warn(LD_DIR, "Dir-signing-key token contained no key");
- }
- done:
- if (tok) token_free(tok);
- if (area) {
- DUMP_AREA(area, "dir-signing-key token");
- memarea_drop_all(area);
- }
- return key;
- }
- /** Return true iff <b>key</b> is allowed to sign directories.
- */
- static int
- dir_signing_key_is_trusted(crypto_pk_env_t *key)
- {
- char digest[DIGEST_LEN];
- if (!key) return 0;
- if (crypto_pk_get_digest(key, digest) < 0) {
- log_warn(LD_DIR, "Error computing dir-signing-key digest");
- return 0;
- }
- if (!router_digest_is_trusted_dir(digest)) {
- log_warn(LD_DIR, "Listed dir-signing-key is not trusted");
- return 0;
- }
- return 1;
- }
- /** 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_CHECK_AUTHORITY</b> is set, make sure that <b>pkey</b> is the key of
- * a directory authority. 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,
- directory_token_t *tok,
- crypto_pk_env_t *pkey,
- int flags,
- const char *doctype)
- {
- char *signed_digest;
- const int check_authority = (flags & CST_CHECK_AUTHORITY);
- const int check_objtype = ! (flags & CST_NO_CHECK_OBJTYPE);
- tor_assert(pkey);
- tor_assert(tok);
- tor_assert(digest);
- tor_assert(doctype);
- if (check_authority && !dir_signing_key_is_trusted(pkey)) {
- log_warn(LD_DIR, "Key on %s did not come from an authority; rejecting",
- doctype);
- return -1;
- }
- if (check_objtype) {
- if (strcmp(tok->object_type, "SIGNATURE")) {
- log_warn(LD_DIR, "Bad object type on %s signature", doctype);
- return -1;
- }
- }
- signed_digest = tor_malloc(tok->object_size);
- if (crypto_pk_public_checksig(pkey, signed_digest, 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 (memcmp(digest, 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
- * we found an extrainfo, or false if found a router. Do not scan beyond
- * <b>eos</b>. Return -1 if we found nothing; 0 if we found something. */
- static int
- find_start_of_next_router_or_extrainfo(const char **s_ptr,
- const char *eos,
- int *is_extrainfo_out)
- {
- const char *annotations = NULL;
- const char *s = *s_ptr;
- s = eat_whitespace_eos(s, eos);
- while (s < eos-32) { /* 32 gives enough room for a the first keyword. */
- /* We're at the start of a line. */
- tor_assert(*s != 'n');
- if (*s == '@' && !annotations) {
- annotations = s;
- } else if (*s == 'r' && !strcmpstart(s, "router ")) {
- *s_ptr = annotations ? annotations : s;
- *is_extrainfo_out = 0;
- return 0;
- } else if (*s == 'e' && !strcmpstart(s, "extra-info ")) {
- *s_ptr = annotations ? annotations : s;
- *is_extrainfo_out = 1;
- return 0;
- }
- if (!(s = memchr(s+1, 'n', eos-(s+1))))
- break;
- s = eat_whitespace_eos(s, eos);
- }
- return -1;
- }
- /** Given a string *<b>s</b> containing a concatenated sequence of router
- * descriptors (or extra-info documents if <b>is_extrainfo</b> is set), parses
- * them and stores the result in <b>dest</b>. All routers are marked running
- * and valid. Advances *s to a point immediately following the last router
- * entry. Ignore any trailing router entries that are not complete.
- *
- * If <b>saved_location</b> isn't SAVED_IN_CACHE, make a local copy of each
- * descriptor in the signed_descriptor_body field of each routerinfo_t. If it
- * isn't SAVED_NOWHERE, remember the offset of each descriptor.
- *
- * Returns 0 on success and -1 on failure.
- */
- int
- router_parse_list_from_string(const char **s, const char *eos,
- smartlist_t *dest,
- saved_location_t saved_location,
- int want_extrainfo,
- int allow_annotations,
- const char *prepend_annotations)
- {
- routerinfo_t *router;
- extrainfo_t *extrainfo;
- signed_descriptor_t *signed_desc;
- void *elt;
- const char *end, *start;
- int have_extrainfo;
- tor_assert(s);
- tor_assert(*s);
- tor_assert(dest);
- start = *s;
- if (!eos)
- eos = *s + strlen(*s);
- tor_assert(eos >= *s);
- while (1) {
- if (find_start_of_next_router_or_extrainfo(s, eos, &have_extrainfo) < 0)
- break;
- end = tor_memstr(*s, eos-*s, "nrouter-signature");
- if (end)
- end = tor_memstr(end, eos-end, "n-----END SIGNATURE-----n");
- if (end)
- end += strlen("n-----END SIGNATURE-----n");
- if (!end)
- break;
- elt = NULL;
- if (have_extrainfo && want_extrainfo) {
- routerlist_t *rl = router_get_routerlist();
- extrainfo = extrainfo_parse_entry_from_string(*s, end,
- saved_location != SAVED_IN_CACHE,
- rl->identity_map);
- if (extrainfo) {
- signed_desc = &extrainfo->cache_info;
- elt = extrainfo;
- }
- } else if (!have_extrainfo && !want_extrainfo) {
- router = router_parse_entry_from_string(*s, end,
- saved_location != SAVED_IN_CACHE,
- allow_annotations,
- prepend_annotations);
- if (router) {
- log_debug(LD_DIR, "Read router '%s', purpose '%s'",
- router->nickname, router_purpose_to_string(router->purpose));
- signed_desc = &router->cache_info;
- elt = router;
- }
- }
- if (!elt) {
- *s = end;
- continue;
- }
- if (saved_location != SAVED_NOWHERE) {
- signed_desc->saved_location = saved_location;
- signed_desc->saved_offset = *s - start;
- }
- *s = end;
- smartlist_add(dest, elt);
- }
- 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();
- log(severity, LD_GENERAL, "%d *distinct* router digests verified",
- digestmap_size(verified_digests));
- #else
- (void)severity; /* suppress "unused parameter" warning */
- #endif
- }
- /** Helper function: reads a single router entry from *<b>s</b> ...
- * *<b>end</b>. Mallocs a new router and returns it if all goes well, else
- * returns NULL. If <b>cache_copy</b> is true, duplicate the contents of
- * s through end into the signed_descriptor_body of the resulting
- * routerinfo_t.
- *
- * If <b>allow_annotations</b>, it's okay to encounter annotations in <b>s</b>
- * before the router; if it's false, reject the router if it's annotated. If
- * <b>prepend_annotations</b> is set, it should contain some annotations:
- * append them to the front of the router before parsing it, and keep them
- * around when caching the router.
- *
- * Only one of allow_annotations and prepend_annotations may be set.
- */
- routerinfo_t *
- router_parse_entry_from_string(const char *s, const char *end,
- int cache_copy, int allow_annotations,
- const char *prepend_annotations)
- {
- routerinfo_t *router = NULL;
- char digest[128];
- smartlist_t *tokens = NULL, *exit_policy_tokens = NULL;
- directory_token_t *tok;
- struct in_addr in;
- const char *start_of_annotations, *cp;
- size_t prepend_len = prepend_annotations ? strlen(prepend_annotations) : 0;
- int ok = 1;
- memarea_t *area = NULL;
- tor_assert(!allow_annotations || !prepend_annotations);
- if (!end) {
- end = s + strlen(s);
- }
- /* point 'end' to a point immediately after the final newline. */
- while (end > s+2 && *(end-1) == 'n' && *(end-2) == 'n')
- --end;
- area = memarea_new();
- tokens = smartlist_create();
- if (prepend_annotations) {
- if (tokenize_string(area,prepend_annotations,NULL,tokens,
- routerdesc_token_table,TS_NOCHECK)) {
- log_warn(LD_DIR, "Error tokenizing router descriptor (annotations).");
- goto err;
- }
- }
- start_of_annotations = s;
- cp = tor_memstr(s, end-s, "nrouter ");
- if (!cp) {
- if (end-s < 7 || strcmpstart(s, "router ")) {
- log_warn(LD_DIR, "No router keyword found.");
- goto err;
- }
- } else {
- s = cp+1;
- }
- if (allow_annotations && start_of_annotations != s) {
- if (tokenize_string(area,start_of_annotations,s,tokens,
- routerdesc_token_table,TS_NOCHECK)) {
- log_warn(LD_DIR, "Error tokenizing router descriptor (annotations).");
- goto err;
- }
- }
- if (router_get_router_hash(s, digest) < 0) {
- log_warn(LD_DIR, "Couldn't compute router hash.");
- goto err;
- }
- {
- int flags = 0;
- if (allow_annotations)
- flags |= TS_ANNOTATIONS_OK;
- if (prepend_annotations)
- flags |= TS_ANNOTATIONS_OK|TS_NO_NEW_ANNOTATIONS;
- if (tokenize_string(area,s,end,tokens,routerdesc_token_table, flags)) {
- log_warn(LD_DIR, "Error tokenizing router descriptor.");
- goto err;
- }
- }
- if (smartlist_len(tokens) < 2) {
- log_warn(LD_DIR, "Impossibly short router descriptor.");
- goto err;
- }
- tok = find_by_keyword(tokens, K_ROUTER);
- tor_assert(tok->n_args >= 5);
- router = tor_malloc_zero(sizeof(routerinfo_t));
- router->country = -1;
- router->cache_info.routerlist_index = -1;
- router->cache_info.annotations_len = s-start_of_annotations + prepend_len;
- router->cache_info.signed_descriptor_len = end-s;
- if (cache_copy) {
- size_t len = router->cache_info.signed_descriptor_len +
- router->cache_info.annotations_len;
- char *cp =
- router->cache_info.signed_descriptor_body = tor_malloc(len+1);
- if (prepend_annotations) {
- memcpy(cp, prepend_annotations, prepend_len);
- cp += prepend_len;
- }
- /* This assertion will always succeed.
- * len == signed_desc_len + annotations_len
- * == end-s + s-start_of_annotations + prepend_len
- * == end-start_of_annotations + prepend_len
- * We already wrote prepend_len bytes into the buffer; now we're
- * writing end-start_of_annotations -NM. */
- tor_assert(cp+(end-start_of_annotations) ==
- router->cache_info.signed_descriptor_body+len);
- memcpy(cp, start_of_annotations, end-start_of_annotations);
- router->cache_info.signed_descriptor_body[len] = ' ';
- tor_assert(strlen(router->cache_info.signed_descriptor_body) == len);
- }
- memcpy(router->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);
- router->nickname = tor_strdup(tok->args[0]);
- if (!is_legal_nickname(router->nickname)) {
- log_warn(LD_DIR,"Router nickname is invalid");
- goto err;
- }
- router->address = tor_strdup(tok->args[1]);
- if (!tor_inet_aton(router->address, &in)) {
- log_warn(LD_DIR,"Router address is not an IP address.");
- goto err;
- }
- router->addr = ntohl(in.s_addr);
- router->or_port =
- (uint16_t) tor_parse_long(tok->args[2],10,0,65535,&ok,NULL);
- if (!ok) {
- log_warn(LD_DIR,"Invalid OR port %s", escaped(tok->args[2]));
- goto err;
- }
- router->dir_port =
- (uint16_t) tor_parse_long(tok->args[4],10,0,65535,&ok,NULL);
- if (!ok) {
- log_warn(LD_DIR,"Invalid dir port %s", escaped(tok->args[4]));
- goto err;
- }
- tok = find_by_keyword(tokens, K_BANDWIDTH);
- tor_assert(tok->n_args >= 3);
- router->bandwidthrate = (int)
- tor_parse_long(tok->args[0],10,1,INT_MAX,&ok,NULL);
- if (!ok) {
- log_warn(LD_DIR, "bandwidthrate %s unreadable or 0. Failing.",
- escaped(tok->args[0]));
- goto err;
- }
- router->bandwidthburst =
- (int) tor_parse_long(tok->args[1],10,0,INT_MAX,&ok,NULL);
- if (!ok) {
- log_warn(LD_DIR, "Invalid bandwidthburst %s", escaped(tok->args[1]));
- goto err;
- }
- router->bandwidthcapacity = (int)
- tor_parse_long(tok->args[2],10,0,INT_MAX,&ok,NULL);
- if (!ok) {
- log_warn(LD_DIR, "Invalid bandwidthcapacity %s", escaped(tok->args[1]));
- goto err;
- }
- if ((tok = find_opt_by_keyword(tokens, A_PURPOSE))) {
- tor_assert(tok->n_args);
- router->purpose = router_purpose_from_string(tok->args[0]);
- } else {
- router->purpose = ROUTER_PURPOSE_GENERAL;
- }
- router->cache_info.send_unencrypted =
- (router->purpose == ROUTER_PURPOSE_GENERAL) ? 1 : 0;
- if ((tok = find_opt_by_keyword(tokens, K_UPTIME))) {
- tor_assert(tok->n_args >= 1);
- router->uptime = tor_parse_long(tok->args[0],10,0,LONG_MAX,&ok,NULL);
- if (!ok) {
- log_warn(LD_DIR, "Invalid uptime %s", escaped(tok->args[0]));
- goto err;
- }
- }
- if ((tok = find_opt_by_keyword(tokens, K_HIBERNATING))) {
- tor_assert(tok->n_args >= 1);
- router->is_hibernating
- = (tor_parse_long(tok->args[0],10,0,LONG_MAX,NULL,NULL) != 0);
- }
- tok = find_by_keyword(tokens, K_PUBLISHED);
- tor_assert(tok->n_args == 1);
- if (parse_iso_time(tok->args[0], &router->cache_info.published_on) < 0)
- goto err;
- tok = find_by_keyword(tokens, K_ONION_KEY);
- router->onion_pkey = tok->key;
- tok->key = NULL; /* Prevent free */
- tok = find_by_keyword(tokens, K_SIGNING_KEY);
- router->identity_pkey = tok->key;
- tok->key = NULL; /* Prevent free */
- if (crypto_pk_get_digest(router->identity_pkey,
- router->cache_info.identity_digest)) {
- log_warn(LD_DIR, "Couldn't calculate key digest"); goto err;
- }
- if ((tok = find_opt_by_keyword(tokens, K_FINGERPRINT))) {
- /* If there's a fingerprint line, it must match the identity digest. */
- char d[DIGEST_LEN];
- tor_assert(tok->n_args == 1);
- tor_strstrip(tok->args[0], " ");
- if (base16_decode(d, DIGEST_LEN, tok->args[0], strlen(tok->args[0]))) {
- log_warn(LD_DIR, "Couldn't decode router fingerprint %s",
- escaped(tok->args[0]));
- goto err;
- }
- if (memcmp(d,router->cache_info.identity_digest, DIGEST_LEN)!=0) {
- log_warn(LD_DIR, "Fingerprint '%s' does not match identity digest.",
- tok->args[0]);
- goto err;
- }
- }
- if ((tok = find_opt_by_keyword(tokens, K_PLATFORM))) {
- router->platform = tor_strdup(tok->args[0]);
- }
- if ((tok = find_opt_by_keyword(tokens, K_CONTACT))) {
- router->contact_info = tor_strdup(tok->args[0]);
- }
- if ((tok = find_opt_by_keyword(tokens, K_EVENTDNS))) {
- router->has_old_dnsworkers = tok->n_args && !strcmp(tok->args[0], "0");
- } else if (router->platform) {
- if (! tor_version_as_new_as(router->platform, "0.1.2.2-alpha"))
- router->has_old_dnsworkers = 1;
- }
- exit_policy_tokens = find_all_exitpolicy(tokens);
- if (!smartlist_len(exit_policy_tokens)) {
- log_warn(LD_DIR, "No exit policy tokens in descriptor.");
- goto err;
- }
- SMARTLIST_FOREACH(exit_policy_tokens, directory_token_t *, t,
- if (router_add_exit_policy(router,t)<0) {
- log_warn(LD_DIR,"Error in exit policy");
- goto err;
- });
- policy_expand_private(&router->exit_policy);
- if (policy_is_reject_star(router->exit_policy))
- router->policy_is_reject_star = 1;
- if ((tok = find_opt_by_keyword(tokens, K_FAMILY)) && tok->n_args) {
- int i;
- router->declared_family = smartlist_create();
- 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 err;
- }
- smartlist_add(router->declared_family, tor_strdup(tok->args[i]));
- }
- }
- if ((tok = find_opt_by_keyword(tokens, K_CACHES_EXTRA_INFO)))
- router->caches_extra_info = 1;
- if ((tok = find_opt_by_keyword(tokens, K_ALLOW_SINGLE_HOP_EXITS)))
- router->allow_single_hop_exits = 1;
- if ((tok = find_opt_by_keyword(tokens, K_EXTRA_INFO_DIGEST))) {
- tor_assert(tok->n_args >= 1);
- if (strlen(tok->args[0]) == HEX_DIGEST_LEN) {
- base16_decode(router->cache_info.extra_info_digest,
- DIGEST_LEN, tok->args[0], HEX_DIGEST_LEN);
- } else {
- log_warn(LD_DIR, "Invalid extra info digest %s", escaped(tok->args[0]));
- }
- }
- if ((tok = find_opt_by_keyword(tokens, K_HIDDEN_SERVICE_DIR))) {
- router->wants_to_be_hs_dir = 1;
- }
- tok = find_by_keyword(tokens, K_ROUTER_SIGNATURE);
- note_crypto_pk_op(VERIFY_RTR);
- #ifdef COUNT_DISTINCT_DIGESTS
- if (!verified_digests)
- verified_digests = digestmap_new();
- digestmap_set(verified_digests, signed_digest, (void*)(uintptr_t)1);
- #endif
- if (check_signature_token(digest, tok, router->identity_pkey, 0,
- "router descriptor") < 0)
- goto err;
- routerinfo_set_country(router);
- if (!router->or_port) {
- log_warn(LD_DIR,"or_port unreadable or 0. Failing.");
- goto err;
- }
- if (!router->platform) {
- router->platform = tor_strdup("<unknown>");
- }
- goto done;
- err:
- routerinfo_free(router);
- router = NULL;
- done:
- if (tokens) {
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
- smartlist_free(tokens);
- }
- if (exit_policy_tokens) {
- smartlist_free(exit_policy_tokens);
- }
- if (area) {
- DUMP_AREA(area, "routerinfo");
- memarea_drop_all(area);
- }
- return router;
- }
- /** Parse a single extrainfo entry from the string <b>s</b>, ending at
- * <b>end</b>. (If <b>end</b> is NULL, parse up to the end of <b>s</b>.) If
- * <b>cache_copy</b> is true, make a copy of the extra-info document in the
- * cache_info fields of the result. If <b>routermap</b> is provided, use it
- * as a map from router identity to routerinfo_t when looking up signing keys.
- */
- extrainfo_t *
- extrainfo_parse_entry_from_string(const char *s, const char *end,
- int cache_copy, struct digest_ri_map_t *routermap)
- {
- extrainfo_t *extrainfo = NULL;
- char digest[128];
- smartlist_t *tokens = NULL;
- directory_token_t *tok;
- crypto_pk_env_t *key = NULL;
- routerinfo_t *router = NULL;
- memarea_t *area = NULL;
- if (!end) {
- end = s + strlen(s);
- }
- /* point 'end' to a point immediately after the final newline. */
- while (end > s+2 && *(end-1) == 'n' && *(end-2) == 'n')
- --end;
- if (router_get_extrainfo_hash(s, digest) < 0) {
- log_warn(LD_DIR, "Couldn't compute router hash.");
- goto err;
- }
- tokens = smartlist_create();
- area = memarea_new();
- if (tokenize_string(area,s,end,tokens,extrainfo_token_table,0)) {
- log_warn(LD_DIR, "Error tokenizing extra-info document.");
- goto err;
- }
- if (smartlist_len(tokens) < 2) {
- log_warn(LD_DIR, "Impossibly short extra-info document.");
- goto err;
- }
- tok = smartlist_get(tokens,0);
- if (tok->tp != K_EXTRA_INFO) {
- log_warn(LD_DIR,"Entry does not start with "extra-info"");
- goto err;
- }
- extrainfo = tor_malloc_zero(sizeof(extrainfo_t));
- extrainfo->cache_info.is_extrainfo = 1;
- if (cache_copy)
- extrainfo->cache_info.signed_descriptor_body = tor_strndup(s, end-s);
- extrainfo->cache_info.signed_descriptor_len = end-s;
- memcpy(extrainfo->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);
- tor_assert(tok->n_args >= 2);
- if (!is_legal_nickname(tok->args[0])) {
- log_warn(LD_DIR,"Bad nickname %s on "extra-info"",escaped(tok->args[0]));
- goto err;
- }
- strlcpy(extrainfo->nickname, tok->args[0], sizeof(extrainfo->nickname));
- if (strlen(tok->args[1]) != HEX_DIGEST_LEN ||
- base16_decode(extrainfo->cache_info.identity_digest, DIGEST_LEN,
- tok->args[1], HEX_DIGEST_LEN)) {
- log_warn(LD_DIR,"Invalid fingerprint %s on "extra-info"",
- escaped(tok->args[1]));
- goto err;
- }
- tok = find_by_keyword(tokens, K_PUBLISHED);
- if (parse_iso_time(tok->args[0], &extrainfo->cache_info.published_on)) {
- log_warn(LD_DIR,"Invalid published time %s on "extra-info"",
- escaped(tok->args[0]));
- goto err;
- }
- if (routermap &&
- (router = digestmap_get((digestmap_t*)routermap,
- extrainfo->cache_info.identity_digest))) {
- key = router->identity_pkey;
- }
- tok = find_by_keyword(tokens, K_ROUTER_SIGNATURE);
- if (strcmp(tok->object_type, "SIGNATURE") ||
- tok->object_size < 128 || tok->object_size > 512) {
- log_warn(LD_DIR, "Bad object type or length on extra-info signature");
- goto err;
- }
- if (key) {
- note_crypto_pk_op(VERIFY_RTR);
- if (check_signature_token(digest, tok, key, 0, "extra-info") < 0)
- goto err;
- if (router)
- extrainfo->cache_info.send_unencrypted =
- router->cache_info.send_unencrypted;
- } else {
- extrainfo->pending_sig = tor_memdup(tok->object_body,
- tok->object_size);
- extrainfo->pending_sig_len = tok->object_size;
- }
- goto done;
- err:
- if (extrainfo)
- extrainfo_free(extrainfo);
- extrainfo = NULL;
- done:
- if (tokens) {
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
- smartlist_free(tokens);
- }
- if (area) {
- DUMP_AREA(area, "extrainfo");
- memarea_drop_all(area);
- }
- 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)
- {
- 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;
- 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;
- tokens = smartlist_create();
- 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, digest, "dir-key-certificate-version",
- "ndir-key-certification", 'n') < 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]))) {
- 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 (memcmp(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);
- /* XXX021 use tor_addr_port_parse() below instead. -RD */
- if (parse_addr_port(LOG_WARN, tok->args[0], &address, NULL,
- &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 &&
- !memcmp(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;
- cert->is_cross_certified = old_cert->is_cross_certified;
- }
- }
- if (!found) {
- if (check_signature_token(digest, tok, cert->identity_key, 0,
- "key certificate")) {
- goto err;
- }
- if ((tok = find_opt_by_keyword(tokens, K_DIR_KEY_CROSSCERT))) {
- /* XXXX Once all authorities generate cross-certified certificates,
- * make this field mandatory. */
- if (check_signature_token(cert->cache_info.identity_digest,
- tok,
- cert->signing_key,
- CST_NO_CHECK_OBJTYPE,
- "key cross-certification")) {
- goto err;
- }
- cert->is_cross_certified = 1;
- }
- }
- 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_free(t));
- smartlist_free(tokens);
- if (area) {
- DUMP_AREA(area, "authority cert");
- memarea_drop_all(area);
- }
- return cert;
- err:
- authority_cert_free(cert);
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(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 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 = strstr(s, "nr ");
- if (eos) {
- const char *eos2 = tor_memstr(s, eos-s, "ndirectory-signature");
- if (eos2 && eos2 < eos)
- return eos2;
- else
- return eos+1;
- } else {
- if ((eos = strstr(s, "ndirectory-signature")))
- return eos+1;
- return s + strlen(s);
- }
- }
- /** 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.
- **/
- 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)
- {
- const char *eos;
- routerstatus_t *rs = NULL;
- directory_token_t *tok;
- char timebuf[ISO_TIME_LEN+1];
- struct in_addr in;
- tor_assert(tokens);
- tor_assert(bool_eq(vote, vote_rs));
- 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 >= 8);
- 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 (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], tok->args[4]) < 0 ||
- parse_iso_time(timebuf, &rs->published_on)<0) {
- log_warn(LD_DIR, "Error parsing time '%s %s'",
- tok->args[3], tok->args[4]);
- goto err;
- }
- if (tor_inet_aton(tok->args[5], &in) == 0) {
- log_warn(LD_DIR, "Error parsing router address in network-status %s",
- escaped(tok->args[5]));
- goto err;
- }
- rs->addr = ntohl(in.s_addr);
- rs->or_port =(uint16_t) tor_parse_long(tok->args[6],10,0,65535,NULL,NULL);
- rs->dir_port = (uint16_t) tor_parse_long(tok->args[7],10,0,65535,NULL,NULL);
- 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 |= (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) {
- 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_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], "V2Dir"))
- rs->is_v2_dir = 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], "BadDirectory"))
- rs->is_bad_directory = 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;
- }
- }
- }
- if ((tok = find_opt_by_keyword(tokens, K_V))) {
- tor_assert(tok->n_args == 1);
- rs->version_known = 1;
- if (strcmpstart(tok->args[0], "Tor ")) {
- rs->version_supports_begindir = 1;
- rs->version_supports_extrainfo_upload = 1;
- rs->version_supports_conditional_consensus = 1;
- } else {
- rs->version_supports_begindir =
- tor_version_as_new_as(tok->args[0], "0.2.0.1-alpha");
- rs->version_supports_extrainfo_upload =
- tor_version_as_new_as(tok->args[0], "0.2.0.0-alpha-dev (r10070)");
- rs->version_supports_v3_dir =
- tor_version_as_new_as(tok->args[0], "0.2.0.8-alpha");
- rs->version_supports_conditional_consensus =
- tor_version_as_new_as(tok->args[0], "0.2.1.1-alpha");
- }
- if (vote_rs) {
- vote_rs->version = tor_strdup(tok->args[0]);
- }
- }
- /* 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 = (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;
- }
- }
- }
- /* 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 (!strcasecmp(rs->nickname, UNNAMED_ROUTER_NICKNAME))
- rs->is_named = 0;
- goto done;
- err:
- if (rs && !vote_rs)
- routerstatus_free(rs);
- rs = NULL;
- done:
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
- smartlist_clear(tokens);
- if (area) {
- DUMP_AREA(area, "routerstatus entry");
- memarea_clear(area);
- }
- *s = eos;
- return rs;
- }
- /** Helper to sort a smartlist of pointers to routerstatus_t */
- static int
- _compare_routerstatus_entries(const void **_a, const void **_b)
- {
- const routerstatus_t *a = *_a, *b = *_b;
- return memcmp(a->identity_digest, b->identity_digest, DIGEST_LEN);
- }
- /** Helper: used in call to _smartlist_uniq to clear out duplicate entries. */
- static void
- _free_duplicate_routerstatus_entry(void *e)
- {
- log_warn(LD_DIR,
- "Network-status has two entries for the same router. "
- "Dropping one.");
- routerstatus_free(e);
- }
- /** Given a v2 network-status object in <b>s</b>, try to
- * parse it and return the result. Return NULL on failure. Check the
- * signature of the network status, but do not (yet) check the signing key for
- * authority.
- */
- networkstatus_v2_t *
- networkstatus_v2_parse_from_string(const char *s)
- {
- const char *eos;
- smartlist_t *tokens = smartlist_create();
- smartlist_t *footer_tokens = smartlist_create();
- networkstatus_v2_t *ns = NULL;
- char ns_digest[DIGEST_LEN];
- char tmp_digest[DIGEST_LEN];
- struct in_addr in;
- directory_token_t *tok;
- int i;
- memarea_t *area = NULL;
- if (router_get_networkstatus_v2_hash(s, ns_digest)) {
- log_warn(LD_DIR, "Unable to compute digest of network-status");
- goto err;
- }
- area = memarea_new();
- eos = find_start_of_next_routerstatus(s);
- if (tokenize_string(area, s, eos, tokens, netstatus_token_table,0)) {
- log_warn(LD_DIR, "Error tokenizing network-status header.");
- goto err;
- }
- ns = tor_malloc_zero(sizeof(networkstatus_v2_t));
- memcpy(ns->networkstatus_digest, ns_digest, DIGEST_LEN);
- tok = find_by_keyword(tokens, K_NETWORK_STATUS_VERSION);
- tor_assert(tok->n_args >= 1);
- if (strcmp(tok->args[0], "2")) {
- log_warn(LD_BUG, "Got a non-v2 networkstatus. Version was "
- "%s", escaped(tok->args[0]));
- goto err;