microdesc_parse.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. log_warn(LD_DIR, "Unparseable microdescriptor");
  141. goto next;
  142. }
  143. if ((tok = find_opt_by_keyword(tokens, A_LAST_LISTED))) {
  144. if (parse_iso_time(tok->args[0], &md->last_listed)) {
  145. log_warn(LD_DIR, "Bad last-listed time in microdescriptor");
  146. goto next;
  147. }
  148. }
  149. tok = find_by_keyword(tokens, K_ONION_KEY);
  150. if (!crypto_pk_public_exponent_ok(tok->key)) {
  151. log_warn(LD_DIR,
  152. "Relay's onion key had invalid exponent.");
  153. goto next;
  154. }
  155. md->onion_pkey = tor_memdup(tok->object_body, tok->object_size);
  156. md->onion_pkey_len = tok->object_size;
  157. crypto_pk_free(tok->key);
  158. if ((tok = find_opt_by_keyword(tokens, K_ONION_KEY_NTOR))) {
  159. curve25519_public_key_t k;
  160. tor_assert(tok->n_args >= 1);
  161. if (curve25519_public_from_base64(&k, tok->args[0]) < 0) {
  162. log_warn(LD_DIR, "Bogus ntor-onion-key in microdesc");
  163. goto next;
  164. }
  165. md->onion_curve25519_pkey =
  166. tor_memdup(&k, sizeof(curve25519_public_key_t));
  167. }
  168. smartlist_t *id_lines = find_all_by_keyword(tokens, K_ID);
  169. if (id_lines) {
  170. SMARTLIST_FOREACH_BEGIN(id_lines, directory_token_t *, t) {
  171. tor_assert(t->n_args >= 2);
  172. if (!strcmp(t->args[0], "ed25519")) {
  173. if (md->ed25519_identity_pkey) {
  174. log_warn(LD_DIR, "Extra ed25519 key in microdesc");
  175. smartlist_free(id_lines);
  176. goto next;
  177. }
  178. ed25519_public_key_t k;
  179. if (ed25519_public_from_base64(&k, t->args[1])<0) {
  180. log_warn(LD_DIR, "Bogus ed25519 key in microdesc");
  181. smartlist_free(id_lines);
  182. goto next;
  183. }
  184. md->ed25519_identity_pkey = tor_memdup(&k, sizeof(k));
  185. }
  186. } SMARTLIST_FOREACH_END(t);
  187. smartlist_free(id_lines);
  188. }
  189. {
  190. smartlist_t *a_lines = find_all_by_keyword(tokens, K_A);
  191. if (a_lines) {
  192. find_single_ipv6_orport(a_lines, &md->ipv6_addr, &md->ipv6_orport);
  193. smartlist_free(a_lines);
  194. }
  195. }
  196. if ((tok = find_opt_by_keyword(tokens, K_FAMILY))) {
  197. md->family = nodefamily_parse(tok->args[0],
  198. NULL,
  199. NF_WARN_MALFORMED);
  200. }
  201. if ((tok = find_opt_by_keyword(tokens, K_P))) {
  202. md->exit_policy = parse_short_policy(tok->args[0]);
  203. }
  204. if ((tok = find_opt_by_keyword(tokens, K_P6))) {
  205. md->ipv6_exit_policy = parse_short_policy(tok->args[0]);
  206. }
  207. smartlist_add(result, md);
  208. okay = 1;
  209. md = NULL;
  210. next:
  211. if (! okay && invalid_digests_out) {
  212. smartlist_add(invalid_digests_out,
  213. tor_memdup(md->digest, DIGEST256_LEN));
  214. }
  215. microdesc_free(md);
  216. md = NULL;
  217. SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
  218. memarea_clear(area);
  219. smartlist_clear(tokens);
  220. s = start_of_next_microdesc;
  221. }
  222. SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
  223. memarea_drop_all(area);
  224. smartlist_free(tokens);
  225. return result;
  226. }