Commit 006b7ce5 authored by Nick Mathewson's avatar Nick Mathewson 🦀
Browse files

Fix the position-check for ed25519 certs to work with annotations

When there are annotations on a router descriptor, the
ed25519-identity element won't be at position 0 or 1; it will be at
router+1 or router-1.

This patch also adds a missing smartlist function to search a list for
an item with a particular pointer.
parent 592a4391
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -208,6 +208,19 @@ smartlist_string_pos(const smartlist_t *sl, const char *element)
  return -1;
}

/** If <b>element</b> is the same pointer as an element of <b>sl</b>, return
 * that element's index.  Otherwise, return -1. */
int
smartlist_pos(const smartlist_t *sl, const void *element)
{
  int i;
  if (!sl) return -1;
  for (i=0; i < sl->num_used; i++)
    if (element == sl->list[i])
      return i;
  return -1;
}

/** Return true iff <b>sl</b> has some element E such that
 * !strcasecmp(E,<b>element</b>)
 */
+1 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ void smartlist_reverse(smartlist_t *sl);
void smartlist_string_remove(smartlist_t *sl, const char *element);
int smartlist_contains(const smartlist_t *sl, const void *element);
int smartlist_contains_string(const smartlist_t *sl, const char *element);
int smartlist_pos(const smartlist_t *sl, const void *element);
int smartlist_string_pos(const smartlist_t *, const char *elt);
int smartlist_contains_string_case(const smartlist_t *sl, const char *element);
int smartlist_contains_int_as_string(const smartlist_t *sl, int num);
+5 −2
Original line number Diff line number Diff line
@@ -1195,6 +1195,7 @@ router_parse_entry_from_string(const char *s, const char *end,
  }

  tok = find_by_keyword(tokens, K_ROUTER);
  const int router_token_pos = smartlist_pos(tokens, tok);
  tor_assert(tok->n_args >= 5);

  router = tor_malloc_zero(sizeof(routerinfo_t));
@@ -1345,8 +1346,10 @@ router_parse_entry_from_string(const char *s, const char *end,
    }
    if (ed_sig_tok) {
      tor_assert(ed_cert_tok && cc_tap_tok && cc_ntor_tok);
      if (ed_cert_tok != smartlist_get(tokens, 0) &&
          ed_cert_tok != smartlist_get(tokens, 1)) {
      const int ed_cert_token_pos = smartlist_pos(tokens, ed_cert_tok);
      if (ed_cert_token_pos == -1 || router_token_pos == -1 ||
          (ed_cert_token_pos != router_token_pos + 1 &&
           ed_cert_token_pos != router_token_pos - 1)) {
        log_warn(LD_DIR, "Ed25519 certificate in wrong position");
        goto err;
      }
+38 −0
Original line number Diff line number Diff line
@@ -495,6 +495,43 @@ test_container_smartlist_join(void *arg)
  tor_free(joined);
}

static void
test_container_smartlist_pos(void *arg)
{
  (void) arg;
  smartlist_t *sl = smartlist_new();

  smartlist_add(sl, tor_strdup("This"));
  smartlist_add(sl, tor_strdup("is"));
  smartlist_add(sl, tor_strdup("a"));
  smartlist_add(sl, tor_strdup("test"));
  smartlist_add(sl, tor_strdup("for"));
  smartlist_add(sl, tor_strdup("a"));
  smartlist_add(sl, tor_strdup("function"));

  /* Test string_pos */
  tt_int_op(smartlist_string_pos(NULL, "Fred"), ==, -1);
  tt_int_op(smartlist_string_pos(sl, "Fred"), ==, -1);
  tt_int_op(smartlist_string_pos(sl, "This"), ==, 0);
  tt_int_op(smartlist_string_pos(sl, "a"), ==, 2);
  tt_int_op(smartlist_string_pos(sl, "function"), ==, 6);

  /* Test pos */
  tt_int_op(smartlist_pos(NULL, "Fred"), ==, -1);
  tt_int_op(smartlist_pos(sl, "Fred"), ==, -1);
  tt_int_op(smartlist_pos(sl, "This"), ==, -1);
  tt_int_op(smartlist_pos(sl, "a"), ==, -1);
  tt_int_op(smartlist_pos(sl, "function"), ==, -1);
  tt_int_op(smartlist_pos(sl, smartlist_get(sl,0)), ==, 0);
  tt_int_op(smartlist_pos(sl, smartlist_get(sl,2)), ==, 2);
  tt_int_op(smartlist_pos(sl, smartlist_get(sl,5)), ==, 5);
  tt_int_op(smartlist_pos(sl, smartlist_get(sl,6)), ==, 6);

 done:
  SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  smartlist_free(sl);
}

static void
test_container_smartlist_ints_eq(void *arg)
{
@@ -1053,6 +1090,7 @@ struct testcase_t container_tests[] = {
  CONTAINER_LEGACY(smartlist_overlap),
  CONTAINER_LEGACY(smartlist_digests),
  CONTAINER_LEGACY(smartlist_join),
  CONTAINER_LEGACY(smartlist_pos),
  CONTAINER(smartlist_ints_eq, 0),
  CONTAINER_LEGACY(bitarray),
  CONTAINER_LEGACY(digestset),