microdesc_parse.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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-2018, 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/relay/router.h"
  19. #include "lib/crypt_ops/crypto_curve25519.h"
  20. #include "lib/crypt_ops/crypto_ed25519.h"
  21. #include "lib/crypt_ops/crypto_format.h"
  22. #include "lib/memarea/memarea.h"
  23. #include "feature/nodelist/microdesc_st.h"
  24. /** List of tokens recognized in microdescriptors */
  25. static token_rule_t microdesc_token_table[] = {
  26. T1_START("onion-key", K_ONION_KEY, NO_ARGS, NEED_KEY_1024),
  27. T01("ntor-onion-key", K_ONION_KEY_NTOR, GE(1), NO_OBJ ),
  28. T0N("id", K_ID, GE(2), NO_OBJ ),
  29. T0N("a", K_A, GE(1), NO_OBJ ),
  30. T01("family", K_FAMILY, ARGS, NO_OBJ ),
  31. T01("p", K_P, CONCAT_ARGS, NO_OBJ ),
  32. T01("p6", K_P6, CONCAT_ARGS, NO_OBJ ),
  33. A01("@last-listed", A_LAST_LISTED, CONCAT_ARGS, NO_OBJ ),
  34. END_OF_TABLE
  35. };
  36. /** Assuming that s starts with a microdesc, return the start of the
  37. * *NEXT* one. Return NULL on "not found." */
  38. static const char *
  39. find_start_of_next_microdesc(const char *s, const char *eos)
  40. {
  41. int started_with_annotations;
  42. s = eat_whitespace_eos(s, eos);
  43. if (!s)
  44. return NULL;
  45. #define CHECK_LENGTH() STMT_BEGIN \
  46. if (eos - s < 32) \
  47. return NULL; \
  48. STMT_END
  49. #define NEXT_LINE() STMT_BEGIN \
  50. s = memchr(s, '\n', eos-s); \
  51. if (!s || eos - s <= 1) \
  52. return NULL; \
  53. s++; \
  54. STMT_END
  55. CHECK_LENGTH();
  56. started_with_annotations = (*s == '@');
  57. if (started_with_annotations) {
  58. /* Start by advancing to the first non-annotation line. */
  59. while (*s == '@')
  60. NEXT_LINE();
  61. }
  62. CHECK_LENGTH();
  63. /* Now we should be pointed at an onion-key line. If we are, then skip
  64. * it. */
  65. if (!strcmpstart(s, "onion-key"))
  66. NEXT_LINE();
  67. /* Okay, now we're pointed at the first line of the microdescriptor which is
  68. not an annotation or onion-key. The next line that _is_ an annotation or
  69. onion-key is the start of the next microdescriptor. */
  70. while (eos - s > 32) {
  71. if (*s == '@' || !strcmpstart(s, "onion-key"))
  72. return s;
  73. NEXT_LINE();
  74. }
  75. return NULL;
  76. #undef CHECK_LENGTH
  77. #undef NEXT_LINE
  78. }
  79. /** Parse as many microdescriptors as are found from the string starting at
  80. * <b>s</b> and ending at <b>eos</b>. If allow_annotations is set, read any
  81. * annotations we recognize and ignore ones we don't.
  82. *
  83. * If <b>saved_location</b> isn't SAVED_IN_CACHE, make a local copy of each
  84. * descriptor in the body field of each microdesc_t.
  85. *
  86. * Return all newly parsed microdescriptors in a newly allocated
  87. * smartlist_t. If <b>invalid_disgests_out</b> is provided, add a SHA256
  88. * microdesc digest to it for every microdesc that we found to be badly
  89. * formed. (This may cause duplicates) */
  90. smartlist_t *
  91. microdescs_parse_from_string(const char *s, const char *eos,
  92. int allow_annotations,
  93. saved_location_t where,
  94. smartlist_t *invalid_digests_out)
  95. {
  96. smartlist_t *tokens;
  97. smartlist_t *result;
  98. microdesc_t *md = NULL;
  99. memarea_t *area;
  100. const char *start = s;
  101. const char *start_of_next_microdesc;
  102. int flags = allow_annotations ? TS_ANNOTATIONS_OK : 0;
  103. const int copy_body = (where != SAVED_IN_CACHE);
  104. directory_token_t *tok;
  105. if (!eos)
  106. eos = s + strlen(s);
  107. s = eat_whitespace_eos(s, eos);
  108. area = memarea_new();
  109. result = smartlist_new();
  110. tokens = smartlist_new();
  111. while (s < eos) {
  112. int okay = 0;
  113. start_of_next_microdesc = find_start_of_next_microdesc(s, eos);
  114. if (!start_of_next_microdesc)
  115. start_of_next_microdesc = eos;
  116. md = tor_malloc_zero(sizeof(microdesc_t));
  117. {
  118. const char *cp = tor_memstr(s, start_of_next_microdesc-s,
  119. "onion-key");
  120. const int no_onion_key = (cp == NULL);
  121. if (no_onion_key) {
  122. cp = s; /* So that we have *some* junk to put in the body */
  123. }
  124. md->bodylen = start_of_next_microdesc - cp;
  125. md->saved_location = where;
  126. if (copy_body)
  127. md->body = tor_memdup_nulterm(cp, md->bodylen);
  128. else
  129. md->body = (char*)cp;
  130. md->off = cp - start;
  131. crypto_digest256(md->digest, md->body, md->bodylen, DIGEST_SHA256);
  132. if (no_onion_key) {
  133. log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Malformed or truncated descriptor");
  134. goto next;
  135. }
  136. }
  137. if (tokenize_string(area, s, start_of_next_microdesc, tokens,
  138. microdesc_token_table, flags)) {
  139. log_warn(LD_DIR, "Unparseable microdescriptor");
  140. goto next;
  141. }
  142. if ((tok = find_opt_by_keyword(tokens, A_LAST_LISTED))) {
  143. if (parse_iso_time(tok->args[0], &md->last_listed)) {
  144. log_warn(LD_DIR, "Bad last-listed time in microdescriptor");
  145. goto next;
  146. }
  147. }
  148. tok = find_by_keyword(tokens, K_ONION_KEY);
  149. if (!crypto_pk_public_exponent_ok(tok->key)) {
  150. log_warn(LD_DIR,
  151. "Relay's onion key had invalid exponent.");
  152. goto next;
  153. }
  154. router_set_rsa_onion_pkey(tok->key, &md->onion_pkey,
  155. &md->onion_pkey_len);
  156. crypto_pk_free(tok->key);
  157. if ((tok = find_opt_by_keyword(tokens, K_ONION_KEY_NTOR))) {
  158. curve25519_public_key_t k;
  159. tor_assert(tok->n_args >= 1);
  160. if (curve25519_public_from_base64(&k, tok->args[0]) < 0) {
  161. log_warn(LD_DIR, "Bogus ntor-onion-key in microdesc");
  162. goto next;
  163. }
  164. md->onion_curve25519_pkey =
  165. tor_memdup(&k, sizeof(curve25519_public_key_t));
  166. }
  167. smartlist_t *id_lines = find_all_by_keyword(tokens, K_ID);
  168. if (id_lines) {
  169. SMARTLIST_FOREACH_BEGIN(id_lines, directory_token_t *, t) {
  170. tor_assert(t->n_args >= 2);
  171. if (!strcmp(t->args[0], "ed25519")) {
  172. if (md->ed25519_identity_pkey) {
  173. log_warn(LD_DIR, "Extra ed25519 key in microdesc");
  174. smartlist_free(id_lines);
  175. goto next;
  176. }
  177. ed25519_public_key_t k;
  178. if (ed25519_public_from_base64(&k, t->args[1])<0) {
  179. log_warn(LD_DIR, "Bogus ed25519 key in microdesc");
  180. smartlist_free(id_lines);
  181. goto next;
  182. }
  183. md->ed25519_identity_pkey = tor_memdup(&k, sizeof(k));
  184. }
  185. } SMARTLIST_FOREACH_END(t);
  186. smartlist_free(id_lines);
  187. }
  188. {
  189. smartlist_t *a_lines = find_all_by_keyword(tokens, K_A);
  190. if (a_lines) {
  191. find_single_ipv6_orport(a_lines, &md->ipv6_addr, &md->ipv6_orport);
  192. smartlist_free(a_lines);
  193. }
  194. }
  195. if ((tok = find_opt_by_keyword(tokens, K_FAMILY))) {
  196. int i;
  197. md->family = smartlist_new();
  198. for (i=0;i<tok->n_args;++i) {
  199. if (!is_legal_nickname_or_hexdigest(tok->args[i])) {
  200. log_warn(LD_DIR, "Illegal nickname %s in family line",
  201. escaped(tok->args[i]));
  202. goto next;
  203. }
  204. smartlist_add_strdup(md->family, tok->args[i]);
  205. }
  206. }
  207. if ((tok = find_opt_by_keyword(tokens, K_P))) {
  208. md->exit_policy = parse_short_policy(tok->args[0]);
  209. }
  210. if ((tok = find_opt_by_keyword(tokens, K_P6))) {
  211. md->ipv6_exit_policy = parse_short_policy(tok->args[0]);
  212. }
  213. smartlist_add(result, md);
  214. okay = 1;
  215. md = NULL;
  216. next:
  217. if (! okay && invalid_digests_out) {
  218. smartlist_add(invalid_digests_out,
  219. tor_memdup(md->digest, DIGEST256_LEN));
  220. }
  221. microdesc_free(md);
  222. md = NULL;
  223. SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
  224. memarea_clear(area);
  225. smartlist_clear(tokens);
  226. s = start_of_next_microdesc;
  227. }
  228. SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
  229. memarea_drop_all(area);
  230. smartlist_free(tokens);
  231. return result;
  232. }