microdesc_parse.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /** Parse as many microdescriptors as are found from the string starting at
  86. * <b>s</b> and ending at <b>eos</b>. If allow_annotations is set, read any
  87. * annotations we recognize and ignore ones we don't.
  88. *
  89. * If <b>saved_location</b> isn't SAVED_IN_CACHE, make a local copy of each
  90. * descriptor in the body field of each microdesc_t.
  91. *
  92. * Return all newly parsed microdescriptors in a newly allocated
  93. * smartlist_t. If <b>invalid_disgests_out</b> is provided, add a SHA256
  94. * microdesc digest to it for every microdesc that we found to be badly
  95. * formed. (This may cause duplicates) */
  96. smartlist_t *
  97. microdescs_parse_from_string(const char *s, const char *eos,
  98. int allow_annotations,
  99. saved_location_t where,
  100. smartlist_t *invalid_digests_out)
  101. {
  102. smartlist_t *tokens;
  103. smartlist_t *result;
  104. microdesc_t *md = NULL;
  105. memarea_t *area;
  106. const char *start = s;
  107. const char *start_of_next_microdesc;
  108. int flags = allow_annotations ? TS_ANNOTATIONS_OK : 0;
  109. const int copy_body = (where != SAVED_IN_CACHE);
  110. directory_token_t *tok;
  111. if (!eos)
  112. eos = s + strlen(s);
  113. s = eat_whitespace_eos(s, eos);
  114. area = memarea_new();
  115. result = smartlist_new();
  116. tokens = smartlist_new();
  117. while (s < eos) {
  118. int okay = 0;
  119. start_of_next_microdesc = find_start_of_next_microdesc(s, eos);
  120. if (!start_of_next_microdesc)
  121. start_of_next_microdesc = eos;
  122. md = tor_malloc_zero(sizeof(microdesc_t));
  123. {
  124. const char *cp = tor_memstr(s, start_of_next_microdesc-s,
  125. "onion-key");
  126. const int no_onion_key = (cp == NULL);
  127. if (no_onion_key) {
  128. cp = s; /* So that we have *some* junk to put in the body */
  129. }
  130. md->bodylen = start_of_next_microdesc - cp;
  131. md->saved_location = where;
  132. if (copy_body)
  133. md->body = tor_memdup_nulterm(cp, md->bodylen);
  134. else
  135. md->body = (char*)cp;
  136. md->off = cp - start;
  137. crypto_digest256(md->digest, md->body, md->bodylen, DIGEST_SHA256);
  138. if (no_onion_key) {
  139. log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Malformed or truncated descriptor");
  140. goto next;
  141. }
  142. }
  143. if (tokenize_string(area, s, start_of_next_microdesc, tokens,
  144. microdesc_token_table, flags)) {
  145. const char *location;
  146. switch (where) {
  147. case SAVED_NOWHERE:
  148. location = "download or generated string";
  149. break;
  150. case SAVED_IN_CACHE:
  151. location = "cache";
  152. break;
  153. case SAVED_IN_JOURNAL:
  154. location = "journal";
  155. break;
  156. default:
  157. location = "unknown location";
  158. break;
  159. }
  160. log_warn(LD_DIR, "Unparseable microdescriptor found in %s", location);
  161. goto next;
  162. }
  163. if ((tok = find_opt_by_keyword(tokens, A_LAST_LISTED))) {
  164. if (parse_iso_time(tok->args[0], &md->last_listed)) {
  165. log_warn(LD_DIR, "Bad last-listed time in microdescriptor");
  166. goto next;
  167. }
  168. }
  169. tok = find_by_keyword(tokens, K_ONION_KEY);
  170. if (!crypto_pk_public_exponent_ok(tok->key)) {
  171. log_warn(LD_DIR,
  172. "Relay's onion key had invalid exponent.");
  173. goto next;
  174. }
  175. md->onion_pkey = tor_memdup(tok->object_body, tok->object_size);
  176. md->onion_pkey_len = tok->object_size;
  177. crypto_pk_free(tok->key);
  178. if ((tok = find_opt_by_keyword(tokens, K_ONION_KEY_NTOR))) {
  179. curve25519_public_key_t k;
  180. tor_assert(tok->n_args >= 1);
  181. if (curve25519_public_from_base64(&k, tok->args[0]) < 0) {
  182. log_warn(LD_DIR, "Bogus ntor-onion-key in microdesc");
  183. goto next;
  184. }
  185. md->onion_curve25519_pkey =
  186. tor_memdup(&k, sizeof(curve25519_public_key_t));
  187. }
  188. smartlist_t *id_lines = find_all_by_keyword(tokens, K_ID);
  189. if (id_lines) {
  190. SMARTLIST_FOREACH_BEGIN(id_lines, directory_token_t *, t) {
  191. tor_assert(t->n_args >= 2);
  192. if (!strcmp(t->args[0], "ed25519")) {
  193. if (md->ed25519_identity_pkey) {
  194. log_warn(LD_DIR, "Extra ed25519 key in microdesc");
  195. smartlist_free(id_lines);
  196. goto next;
  197. }
  198. ed25519_public_key_t k;
  199. if (ed25519_public_from_base64(&k, t->args[1])<0) {
  200. log_warn(LD_DIR, "Bogus ed25519 key in microdesc");
  201. smartlist_free(id_lines);
  202. goto next;
  203. }
  204. md->ed25519_identity_pkey = tor_memdup(&k, sizeof(k));
  205. }
  206. } SMARTLIST_FOREACH_END(t);
  207. smartlist_free(id_lines);
  208. }
  209. {
  210. smartlist_t *a_lines = find_all_by_keyword(tokens, K_A);
  211. if (a_lines) {
  212. find_single_ipv6_orport(a_lines, &md->ipv6_addr, &md->ipv6_orport);
  213. smartlist_free(a_lines);
  214. }
  215. }
  216. if ((tok = find_opt_by_keyword(tokens, K_FAMILY))) {
  217. md->family = nodefamily_parse(tok->args[0],
  218. NULL,
  219. NF_WARN_MALFORMED);
  220. }
  221. if ((tok = find_opt_by_keyword(tokens, K_P))) {
  222. md->exit_policy = parse_short_policy(tok->args[0]);
  223. }
  224. if ((tok = find_opt_by_keyword(tokens, K_P6))) {
  225. md->ipv6_exit_policy = parse_short_policy(tok->args[0]);
  226. }
  227. if (policy_is_reject_star_or_null(md->exit_policy) &&
  228. policy_is_reject_star_or_null(md->ipv6_exit_policy)) {
  229. md->policy_is_reject_star = 1;
  230. }
  231. smartlist_add(result, md);
  232. okay = 1;
  233. md = NULL;
  234. next:
  235. if (! okay && invalid_digests_out) {
  236. smartlist_add(invalid_digests_out,
  237. tor_memdup(md->digest, DIGEST256_LEN));
  238. }
  239. microdesc_free(md);
  240. md = NULL;
  241. SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
  242. memarea_clear(area);
  243. smartlist_clear(tokens);
  244. s = start_of_next_microdesc;
  245. }
  246. SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
  247. memarea_drop_all(area);
  248. smartlist_free(tokens);
  249. return result;
  250. }