/* Copyright (c) 2001 Matej Pfajfar.
* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2019, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
* \file routerparse.c
* \brief Code to parse and validate router descriptors, consenus directories,
* and similar objects.
*
* The objects parsed by this module use a common text-based metaformat,
* documented in dir-spec.txt in torspec.git. This module is itself divided
* into two major kinds of function: code to handle the metaformat, and code
* to convert from particular instances of the metaformat into the
* objects that Tor uses.
*
* The generic parsing code works by calling a table-based tokenizer on the
* input string. Each token corresponds to a single line with a token, plus
* optional arguments on that line, plus an optional base-64 encoded object
* after that line. Each token has a definition in a table of token_rule_t
* entries that describes how many arguments it can take, whether it takes an
* object, how many times it may appear, whether it must appear first, and so
* on.
*
* The tokenizer function tokenize_string() converts its string input into a
* smartlist full of instances of directory_token_t, according to a provided
* table of token_rule_t.
*
* The generic parts of this module additionally include functions for
* finding the start and end of signed information inside a signed object, and
* computing the digest that will be signed.
*
* There are also functions for saving objects to disk that have caused
* parsing to fail.
*
* The specific parts of this module describe conversions between
* particular lists of directory_token_t and particular objects. The
* kinds of objects that can be parsed here are:
* <ul>
* <li>router descriptors (managed from routerlist.c)
* <li>extra-info documents (managed from routerlist.c)
* <li>microdescriptors (managed from microdesc.c)
* <li>vote and consensus networkstatus documents, and the routerstatus_t
* objects that they comprise (managed from networkstatus.c)
* <li>detached-signature objects used by authorities for gathering
* signatures on the networkstatus consensus (managed from dirvote.c)
* <li>authority key certificates (managed from routerlist.c)
* <li>hidden service descriptors (managed from rendcommon.c and rendcache.c)
* </ul>
**/
#define EXPOSE_ROUTERDESC_TOKEN_TABLE
#include "core/or/or.h"
#include "app/config/config.h"
#include "core/or/policies.h"
#include "core/or/versions.h"
#include "feature/dirparse/parsecommon.h"
#include "feature/dirparse/policy_parse.h"
#include "feature/dirparse/routerparse.h"
#include "feature/dirparse/sigcommon.h"
#include "feature/dirparse/unparseable.h"
#include "feature/nodelist/describe.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 "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 "lib/sandbox/sandbox.h"
#include "core/or/addr_policy_st.h"
#include "feature/nodelist/extrainfo_st.h"
#include "feature/nodelist/routerinfo_st.h"
#include "feature/nodelist/routerlist_st.h"
/****************************************************************************/
/** List of tokens recognized in router descriptors */
const 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 ),
T01("ipv6-policy", K_IPV6_POLICY, CONCAT_ARGS, NO_OBJ),
T1( "signing-key", K_SIGNING_KEY, NO_ARGS, NEED_KEY_1024 ),
T1( "onion-key", K_ONION_KEY, NO_ARGS, NEED_KEY_1024 ),
T01("ntor-onion-key", K_ONION_KEY_NTOR, GE(1), NO_OBJ<