microdesc_parse.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file microdesc_parse.c
  8. * \brief Code to parse and validate microdescriptors.
  9. **/
  10. #include "core/or/or.h"
  11. #include "app/config/config.h"
  12. #include "core/or/policies.h"
  13. #include "feature/dirparse/microdesc_parse.h"
  14. #include "feature/dirparse/parsecommon.h"
  15. #include "feature/dirparse/routerparse.h"
  16. #include "feature/nodelist/microdesc.h"
  17. #include "feature/nodelist/nickname.h"
  18. #include "feature/nodelist/nodefamily.h"
  19. #include "feature/relay/router.h"
  20. #include "lib/crypt_ops/crypto_curve25519.h"
  21. #include "lib/crypt_ops/crypto_ed25519.h"
  22. #include "lib/crypt_ops/crypto_format.h"
  23. #include "lib/memarea/memarea.h"
  24. #include "feature/nodelist/microdesc_st.h"
  25. /** List of tokens recognized in microdescriptors */
  26. static token_rule_t microdesc_token_table[] = {
  27. T1_START("onion-key", K_ONION_KEY, NO_ARGS, NEED_KEY_1024),
  28. T01("ntor-onion-key", K_ONION_KEY_NTOR, GE(1), NO_OBJ ),
  29. T0N("id", K_ID, GE(2), NO_OBJ ),
  30. T0N("a", K_A, GE(1), NO_OBJ ),
  31. T01("family", K_FAMILY, CONCAT_ARGS, NO_OBJ ),
  32. T01("p", K_P, CONCAT_ARGS, NO_OBJ ),
  33. T01("p6", K_P6, CONCAT_ARGS, NO_OBJ ),
  34. A01("@last-listed", A_LAST_LISTED, CONCAT_ARGS, NO_OBJ ),
  35. END_OF_TABLE
  36. };
  37. /** Assuming that s starts with a microdesc, return the start of the
  38. * *NEXT* one. Return NULL on "not found." */
  39. static const char *
  40. find_start_of_next_microdesc(const char *s, const char *eos)
  41. {
  42. int started_with_annotations;
  43. s = eat_whitespace_eos(s, eos);
  44. if (!s)
  45. return NULL;
  46. #define CHECK_LENGTH() STMT_BEGIN \
  47. if (eos - s < 32) \
  48. return NULL; \
  49. STMT_END
  50. #define NEXT_LINE() STMT_BEGIN \
  51. s = memchr(s, '\n', eos-s); \
  52. if (!s || eos - s <= 1) \
  53. return NULL; \
  54. s++; \
  55. STMT_END
  56. CHECK_LENGTH();
  57. started_with_annotations = (*s == '@');
  58. if (started_with_annotations) {
  59. /* Start by advancing to the first non-annotation line. */
  60. while (*s == '@')
  61. NEXT_LINE();
  62. }
  63. CHECK_LENGTH();
  64. /* Now we should be pointed at an onion-key line. If we are, then skip
  65. * it. */
  66. if (!strcmpstart(s, "onion-key"))
  67. NEXT_LINE();
  68. /* Okay, now we're pointed at the first line of the microdescriptor which is
  69. not an annotation or onion-key. The next line that _is_ an annotation or
  70. onion-key is the start of the next microdescriptor. */
  71. while (eos - s > 32) {
  72. if (*s == '@' || !strcmpstart(s, "onion-key"))
  73. return s;
  74. NEXT_LINE();
  75. }
  76. return NULL;
  77. #undef CHECK_LENGTH
  78. #undef NEXT_LINE
  79. }
  80. static inline int
  81. policy_is_reject_star_or_null(struct short_policy_t *policy)
  82. {
  83. return !policy || short_policy_is_reject_star(policy);
  84. }
  85. /**
  86. * Return a human-readable description of a given saved_location_t.
  87. * Never returns NULL.
  88. **/
  89. static const char *
  90. saved_location_to_string(saved_location_t where)
  91. {
  92. const char *location;
  93. switch (where) {
  94. case SAVED_NOWHERE:
  95. location = "download or generated string";
  96. break;
  97. case SAVED_IN_CACHE:
  98. location = "cache";
  99. break;
  100. case SAVED_IN_JOURNAL:
  101. location = "journal";
  102. break;
  103. default:
  104. location = "unknown location";
  105. break;
  106. }
  107. return location;
  108. }
  109. /**
  110. * Given a microdescriptor stored in <b>where</b> which starts at <b>s</b>,
  111. * which ends at <b>start_of_next_microdescriptor</b>, and which is located
  112. * within a larger document beginning at <b>start</b>: Fill in the body,
  113. * bodylen, bodylen, saved_location, off, and digest fields of <b>md</b> as
  114. * appropriate.
  115. *
  116. * The body field will be an alias within <b>s</b> if <b>saved_location</b>
  117. * is SAVED_IN_CACHE, and will be copied into body and nul-terminated
  118. * otherwise.
  119. **/
  120. static int
  121. microdesc_extract_body(microdesc_t *md,
  122. const char *start,
  123. const char *s, const char *start_of_next_microdesc,
  124. saved_location_t where)
  125. {
  126. const bool copy_body = (where != SAVED_IN_CACHE);
  127. const char *cp = tor_memstr(s, start_of_next_microdesc-s, "onion-key");
  128. const bool no_onion_key = (cp == NULL);
  129. if (no_onion_key) {
  130. cp = s; /* So that we have *some* junk to put in the body */
  131. }
  132. md->bodylen = start_of_next_microdesc - cp;
  133. md->saved_location = where;
  134. if (copy_body)
  135. md->body = tor_memdup_nulterm(cp, md->bodylen);
  136. else
  137. md->body = (char*)cp;
  138. md->off = cp - start;
  139. crypto_digest256(md->digest, md->body, md->bodylen, DIGEST_SHA256);
  140. return no_onion_key ? -1 : 0;
  141. }
  142. /**
  143. * Parse a microdescriptor which begins at <b>s</b> and ends at
  144. * <b>start_of_next_microdesc. Store its fields into <b>md</b>. Use
  145. * <b>where</b> for generating log information. If <b>allow_annotations</b>
  146. * is true, then one or more annotations may precede the microdescriptor body
  147. * proper. Use <b>area</b> for memory management, clearing it when done.
  148. *
  149. * On success, return 0; otherwise return -1.
  150. **/
  151. static int
  152. microdesc_parse_fields(microdesc_t *md,
  153. memarea_t *area,
  154. const char *s, const char *start_of_next_microdesc,
  155. int allow_annotations,
  156. saved_location_t where)
  157. {
  158. smartlist_t *tokens = smartlist_new();
  159. int rv = -1;
  160. int flags = allow_annotations ? TS_ANNOTATIONS_OK : 0;
  161. directory_token_t *tok;
  162. if (tokenize_string(area, s, start_of_next_microdesc, tokens,
  163. microdesc_token_table, flags)) {
  164. log_warn(LD_DIR, "Unparseable microdescriptor found in %s",
  165. saved_location_to_string(where));
  166. goto err;
  167. }
  168. if ((tok = find_opt_by_keyword(tokens, A_LAST_LISTED))) {
  169. if (parse_iso_time(tok->args[0], &md->last_listed)) {
  170. log_warn(LD_DIR, "Bad last-listed time in microdescriptor");
  171. goto err;
  172. }
  173. }
  174. tok = find_by_keyword(tokens, K_ONION_KEY);
  175. if (!crypto_pk_public_exponent_ok(tok->key)) {
  176. log_warn(LD_DIR,
  177. "Relay's onion key had invalid exponent.");
  178. goto err;
  179. }
  180. md->onion_pkey = tor_memdup(tok->object_body, tok->object_size);
  181. md->onion_pkey_len = tok->object_size;
  182. crypto_pk_free(tok->key);
  183. if ((tok = find_opt_by_keyword(tokens, K_ONION_KEY_NTOR))) {
  184. curve25519_public_key_t k;
  185. tor_assert(tok->n_args >= 1);
  186. if (curve25519_public_from_base64(&k, tok->args[0]) < 0) {
  187. log_warn(LD_DIR, "Bogus ntor-onion-key in microdesc");
  188. goto err;
  189. }
  190. md->onion_curve25519_pkey =
  191. tor_memdup(&k, sizeof(curve25519_public_key_t));
  192. }
  193. smartlist_t *id_lines = find_all_by_keyword(tokens, K_ID);
  194. if (id_lines) {
  195. SMARTLIST_FOREACH_BEGIN(id_lines, directory_token_t *, t) {
  196. tor_assert(t->n_args >= 2);
  197. if (!strcmp(t->args[0], "ed25519")) {
  198. if (md->ed25519_identity_pkey) {
  199. log_warn(LD_DIR, "Extra ed25519 key in microdesc");
  200. smartlist_free(id_lines);
  201. goto err;
  202. }
  203. ed25519_public_key_t k;
  204. if (ed25519_public_from_base64(&k, t->args[1])<0) {
  205. log_warn(LD_DIR, "Bogus ed25519 key in microdesc");
  206. smartlist_free(id_lines);
  207. goto err;
  208. }
  209. md->ed25519_identity_pkey = tor_memdup(&k, sizeof(k));
  210. }
  211. } SMARTLIST_FOREACH_END(t);
  212. smartlist_free(id_lines);
  213. }
  214. {
  215. smartlist_t *a_lines = find_all_by_keyword(tokens, K_A);
  216. if (a_lines) {
  217. find_single_ipv6_orport(a_lines, &md->ipv6_addr, &md->ipv6_orport);
  218. smartlist_free(a_lines);
  219. }
  220. }
  221. if ((tok = find_opt_by_keyword(tokens, K_FAMILY))) {
  222. md->family = nodefamily_parse(tok->args[0],
  223. NULL,
  224. NF_WARN_MALFORMED);
  225. }
  226. if ((tok = find_opt_by_keyword(tokens, K_P))) {
  227. md->exit_policy = parse_short_policy(tok->args[0]);
  228. }
  229. if ((tok = find_opt_by_keyword(tokens, K_P6))) {
  230. md->ipv6_exit_policy = parse_short_policy(tok->args[0]);
  231. }
  232. if (policy_is_reject_star_or_null(md->exit_policy) &&
  233. policy_is_reject_star_or_null(md->ipv6_exit_policy)) {
  234. md->policy_is_reject_star = 1;
  235. }
  236. rv = 0;
  237. err:
  238. SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
  239. memarea_clear(area);
  240. smartlist_free(tokens);
  241. return rv;
  242. }
  243. /** Parse as many microdescriptors as are found from the string starting at
  244. * <b>s</b> and ending at <b>eos</b>. If allow_annotations is set, read any
  245. * annotations we recognize and ignore ones we don't.
  246. *
  247. * If <b>saved_location</b> isn't SAVED_IN_CACHE, make a local copy of each
  248. * descriptor in the body field of each microdesc_t.
  249. *
  250. * Return all newly parsed microdescriptors in a newly allocated
  251. * smartlist_t. If <b>invalid_disgests_out</b> is provided, add a SHA256
  252. * microdesc digest to it for every microdesc that we found to be badly
  253. * formed. (This may cause duplicates) */
  254. smartlist_t *
  255. microdescs_parse_from_string(const char *s, const char *eos,
  256. int allow_annotations,
  257. saved_location_t where,
  258. smartlist_t *invalid_digests_out)
  259. {
  260. smartlist_t *result;
  261. microdesc_t *md = NULL;
  262. memarea_t *area;
  263. const char *start = s;
  264. const char *start_of_next_microdesc;
  265. if (!eos)
  266. eos = s + strlen(s);
  267. s = eat_whitespace_eos(s, eos);
  268. area = memarea_new();
  269. result = smartlist_new();
  270. while (s < eos) {
  271. bool okay = false;
  272. start_of_next_microdesc = find_start_of_next_microdesc(s, eos);
  273. if (!start_of_next_microdesc)
  274. start_of_next_microdesc = eos;
  275. md = tor_malloc_zero(sizeof(microdesc_t));
  276. uint8_t md_digest[DIGEST256_LEN];
  277. {
  278. const bool body_not_found =
  279. microdesc_extract_body(md, start, s,
  280. start_of_next_microdesc,
  281. where) < 0;
  282. memcpy(md_digest, md->digest, DIGEST256_LEN);
  283. if (body_not_found) {
  284. log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Malformed or truncated descriptor");
  285. goto next;
  286. }
  287. }
  288. if (microdesc_parse_fields(md, area, s, start_of_next_microdesc,
  289. allow_annotations, where) == 0) {
  290. smartlist_add(result, md);
  291. md = NULL; // prevent free
  292. okay = true;
  293. }
  294. next:
  295. if (! okay && invalid_digests_out) {
  296. smartlist_add(invalid_digests_out,
  297. tor_memdup(md_digest, DIGEST256_LEN));
  298. }
  299. microdesc_free(md);
  300. md = NULL;
  301. s = start_of_next_microdesc;
  302. }
  303. memarea_drop_all(area);
  304. return result;
  305. }