Commit 04bb7019 authored by Nick Mathewson's avatar Nick Mathewson 🦞
Browse files

Followup: Make authority_cert_parse_from_string() take length too

parent 7e3005af
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -555,7 +555,8 @@ trusted_dirs_load_certs_from_string(const char *contents, int source,
  int added_trusted_cert = 0;

  for (s = contents; *s; s = eos) {
    authority_cert_t *cert = authority_cert_parse_from_string(s, &eos);
    authority_cert_t *cert = authority_cert_parse_from_string(s, strlen(s),
                                                              &eos);
    cert_list_t *cl;
    if (!cert) {
      failure_code = -1;
+11 −8
Original line number Diff line number Diff line
@@ -2308,7 +2308,8 @@ extrainfo_parse_entry_from_string(const char *s, const char *end,
/** Parse a key certificate from <b>s</b>; point <b>end-of-string</b> to
 * the first character after the certificate. */
authority_cert_t *
authority_cert_parse_from_string(const char *s, const char **end_of_string)
authority_cert_parse_from_string(const char *s, size_t maxlen,
                                 const char **end_of_string)
{
  /** Reject any certificate at least this big; it is probably an overflow, an
   * attack, a bug, or some other nonsense. */
@@ -2319,24 +2320,25 @@ authority_cert_parse_from_string(const char *s, const char **end_of_string)
  char digest[DIGEST_LEN];
  directory_token_t *tok;
  char fp_declared[DIGEST_LEN];
  char *eos;
  const char *eos;
  size_t len;
  int found;
  memarea_t *area = NULL;
  const char *end_of_s = s + maxlen;
  const char *s_dup = s;

  s = eat_whitespace(s);
  eos = strstr(s, "\ndir-key-certification");
  s = eat_whitespace_eos(s, end_of_s);
  eos = tor_memstr(s, end_of_s - s, "\ndir-key-certification");
  if (! eos) {
    log_warn(LD_DIR, "No signature found on key certificate");
    return NULL;
  }
  eos = strstr(eos, "\n-----END SIGNATURE-----\n");
  eos = tor_memstr(eos, end_of_s - eos, "\n-----END SIGNATURE-----\n");
  if (! eos) {
    log_warn(LD_DIR, "No end-of-signature found on key certificate");
    return NULL;
  }
  eos = strchr(eos+2, '\n');
  eos = memchr(eos+2, '\n', end_of_s - (eos+2));
  tor_assert(eos);
  ++eos;
  len = eos - s;
@@ -2353,7 +2355,7 @@ authority_cert_parse_from_string(const char *s, const char **end_of_string)
    log_warn(LD_DIR, "Error tokenizing key certificate");
    goto err;
  }
  if (router_get_hash_impl(s, strlen(s), digest, "dir-key-certificate-version",
  if (router_get_hash_impl(s, eos-s, digest, "dir-key-certificate-version",
                           "\ndir-key-certification", '\n', DIGEST_SHA1) < 0)
    goto err;
  tok = smartlist_get(tokens, 0);
@@ -3465,7 +3467,8 @@ networkstatus_parse_vote_from_string(const char *s,
                            "\ndir-key-certificate-version")))
      goto err;
    ++cert;
    ns->cert = authority_cert_parse_from_string(cert, &end_of_cert);
    ns->cert = authority_cert_parse_from_string(cert, end_of_header - cert,
                                                &end_of_cert);
    if (!ns->cert || !end_of_cert || end_of_cert > end_of_header)
      goto err;
  }
+1 −0
Original line number Diff line number Diff line
@@ -93,6 +93,7 @@ smartlist_t *microdescs_parse_from_string(const char *s, const char *eos,
                                          smartlist_t *invalid_digests_out);

authority_cert_t *authority_cert_parse_from_string(const char *s,
                                                   size_t maxlen,
                                                   const char **end_of_string);
int rend_parse_v2_service_descriptor(rend_service_descriptor_t **parsed_out,
                                     char *desc_id_out,
+1 −1
Original line number Diff line number Diff line
@@ -717,7 +717,7 @@ load_authority_keyset(int legacy, crypto_pk_t **key_out,
               fname);
    goto done;
  }
  parsed = authority_cert_parse_from_string(cert, &eos);
  parsed = authority_cert_parse_from_string(cert, strlen(cert), &eos);
  if (!parsed) {
    log_warn(LD_DIR, "Unable to parse certificate in %s", fname);
    goto done;
+9 −3
Original line number Diff line number Diff line
@@ -2799,11 +2799,17 @@ test_a_networkstatus(
  MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);

  /* Parse certificates and keys. */
  cert1 = mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  cert1 = mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1,
                                                 strlen(AUTHORITY_CERT_1),
                                                 NULL);
  tt_assert(cert1);
  cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL);
  cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2,
                                           strlen(AUTHORITY_CERT_2),
                                           NULL);
  tt_assert(cert2);
  cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3, NULL);
  cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3,
                                           strlen(AUTHORITY_CERT_3),
                                           NULL);
  tt_assert(cert3);
  sign_skey_1 = crypto_pk_new();
  sign_skey_2 = crypto_pk_new();
Loading