microdesc_parse.c 8.6 KB

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