Merge branch 'bug14013_029_01_squashed'

This commit is contained in:
Nick Mathewson 2016-06-20 08:48:09 -04:00
commit 6cedd49323
17 changed files with 89 additions and 58 deletions

View File

@ -515,18 +515,21 @@ hex_decode_digit(char c)
return hex_decode_digit_(c); return hex_decode_digit_(c);
} }
/** Given a hexadecimal string of <b>srclen</b> bytes in <b>src</b>, decode it /** Given a hexadecimal string of <b>srclen</b> bytes in <b>src</b>, decode
* and store the result in the <b>destlen</b>-byte buffer at <b>dest</b>. * it and store the result in the <b>destlen</b>-byte buffer at <b>dest</b>.
* Return 0 on success, -1 on failure. */ * Return the number of bytes decoded on success, -1 on failure. If
* <b>destlen</b> is greater than INT_MAX or less than half of
* <b>srclen</b>, -1 is returned. */
int int
base16_decode(char *dest, size_t destlen, const char *src, size_t srclen) base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
{ {
const char *end; const char *end;
char *dest_orig = dest;
int v1,v2; int v1,v2;
if ((srclen % 2) != 0) if ((srclen % 2) != 0)
return -1; return -1;
if (destlen < srclen/2 || destlen > SIZE_T_CEILING) if (destlen < srclen/2 || destlen > INT_MAX)
return -1; return -1;
memset(dest, 0, destlen); memset(dest, 0, destlen);
@ -541,6 +544,9 @@ base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
++dest; ++dest;
src+=2; src+=2;
} }
return 0;
tor_assert((dest-dest_orig) <= (ptrdiff_t) destlen);
return (int) (dest-dest_orig);
} }

View File

@ -2028,7 +2028,8 @@ circuit_get_open_circ_or_launch(entry_connection_t *conn,
char *hexdigest = conn->chosen_exit_name+1; char *hexdigest = conn->chosen_exit_name+1;
tor_addr_t addr; tor_addr_t addr;
if (strlen(hexdigest) < HEX_DIGEST_LEN || if (strlen(hexdigest) < HEX_DIGEST_LEN ||
base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN)<0) { base16_decode(digest,DIGEST_LEN,
hexdigest,HEX_DIGEST_LEN) != DIGEST_LEN) {
log_info(LD_DIR, "Broken exit digest on tunnel conn. Closing."); log_info(LD_DIR, "Broken exit digest on tunnel conn. Closing.");
return -1; return -1;
} }

View File

@ -5330,7 +5330,7 @@ parse_bridge_line(const char *line)
goto err; goto err;
} }
if (base16_decode(bridge_line->digest, DIGEST_LEN, if (base16_decode(bridge_line->digest, DIGEST_LEN,
fingerprint, HEX_DIGEST_LEN)<0) { fingerprint, HEX_DIGEST_LEN) != DIGEST_LEN) {
log_warn(LD_CONFIG, "Unable to decode Bridge key digest."); log_warn(LD_CONFIG, "Unable to decode Bridge key digest.");
goto err; goto err;
} }
@ -5781,7 +5781,8 @@ parse_dir_authority_line(const char *line, dirinfo_type_t required_type,
} else if (!strcasecmpstart(flag, "v3ident=")) { } else if (!strcasecmpstart(flag, "v3ident=")) {
char *idstr = flag + strlen("v3ident="); char *idstr = flag + strlen("v3ident=");
if (strlen(idstr) != HEX_DIGEST_LEN || if (strlen(idstr) != HEX_DIGEST_LEN ||
base16_decode(v3_digest, DIGEST_LEN, idstr, HEX_DIGEST_LEN)<0) { base16_decode(v3_digest, DIGEST_LEN,
idstr, HEX_DIGEST_LEN) != DIGEST_LEN) {
log_warn(LD_CONFIG, "Bad v3 identity digest '%s' on DirAuthority line", log_warn(LD_CONFIG, "Bad v3 identity digest '%s' on DirAuthority line",
flag); flag);
} else { } else {
@ -5830,7 +5831,8 @@ parse_dir_authority_line(const char *line, dirinfo_type_t required_type,
fingerprint, (int)strlen(fingerprint)); fingerprint, (int)strlen(fingerprint));
goto err; goto err;
} }
if (base16_decode(digest, DIGEST_LEN, fingerprint, HEX_DIGEST_LEN)<0) { if (base16_decode(digest, DIGEST_LEN,
fingerprint, HEX_DIGEST_LEN) != DIGEST_LEN) {
log_warn(LD_CONFIG, "Unable to decode DirAuthority key digest."); log_warn(LD_CONFIG, "Unable to decode DirAuthority key digest.");
goto err; goto err;
} }
@ -5898,8 +5900,8 @@ parse_dir_fallback_line(const char *line,
orport = (int)tor_parse_long(cp+strlen("orport="), 10, orport = (int)tor_parse_long(cp+strlen("orport="), 10,
1, 65535, &ok, NULL); 1, 65535, &ok, NULL);
} else if (!strcmpstart(cp, "id=")) { } else if (!strcmpstart(cp, "id=")) {
ok = !base16_decode(id, DIGEST_LEN, ok = base16_decode(id, DIGEST_LEN, cp+strlen("id="),
cp+strlen("id="), strlen(cp)-strlen("id=")); strlen(cp)-strlen("id=")) == DIGEST_LEN;
} else if (!strcasecmpstart(cp, "ipv6=")) { } else if (!strcasecmpstart(cp, "ipv6=")) {
if (ipv6_addrport_ptr) { if (ipv6_addrport_ptr) {
log_warn(LD_CONFIG, "Redundant ipv6 addr/port on FallbackDir line"); log_warn(LD_CONFIG, "Redundant ipv6 addr/port on FallbackDir line");

View File

@ -1211,7 +1211,8 @@ decode_hashed_passwords(config_line_t *passwords)
const char *hashed = cl->value; const char *hashed = cl->value;
if (!strcmpstart(hashed, "16:")) { if (!strcmpstart(hashed, "16:")) {
if (base16_decode(decoded, sizeof(decoded), hashed+3, strlen(hashed+3))<0 if (base16_decode(decoded, sizeof(decoded), hashed+3, strlen(hashed+3))
!= S2K_RFC2440_SPECIFIER_LEN + DIGEST_LEN
|| strlen(hashed+3) != (S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN)*2) { || strlen(hashed+3) != (S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN)*2) {
goto err; goto err;
} }
@ -1262,7 +1263,8 @@ handle_control_authenticate(control_connection_t *conn, uint32_t len,
tor_assert(i>0); tor_assert(i>0);
password_len = i/2; password_len = i/2;
password = tor_malloc(password_len + 1); password = tor_malloc(password_len + 1);
if (base16_decode(password, password_len+1, body, i)<0) { if (base16_decode(password, password_len+1, body, i)
!= (int) (password_len+1)) {
connection_write_str_to_buf( connection_write_str_to_buf(
"551 Invalid hexadecimal encoding. Maybe you tried a plain text " "551 Invalid hexadecimal encoding. Maybe you tried a plain text "
"password? If so, the standard requires that you put it in " "password? If so, the standard requires that you put it in "
@ -2026,7 +2028,8 @@ getinfo_helper_dir(control_connection_t *control_conn,
if (strlen(question) == HEX_DIGEST_LEN) { if (strlen(question) == HEX_DIGEST_LEN) {
char d[DIGEST_LEN]; char d[DIGEST_LEN];
signed_descriptor_t *sd = NULL; signed_descriptor_t *sd = NULL;
if (base16_decode(d, sizeof(d), question, strlen(question))==0) { if (base16_decode(d, sizeof(d), question, strlen(question))
!= sizeof(d)) {
/* XXXX this test should move into extrainfo_get_by_descriptor_digest, /* XXXX this test should move into extrainfo_get_by_descriptor_digest,
* but I don't want to risk affecting other parts of the code, * but I don't want to risk affecting other parts of the code,
* especially since the rules for using our own extrainfo (including * especially since the rules for using our own extrainfo (including
@ -3442,7 +3445,8 @@ handle_control_authchallenge(control_connection_t *conn, uint32_t len,
client_nonce = tor_malloc_zero(client_nonce_len); client_nonce = tor_malloc_zero(client_nonce_len);
if (base16_decode(client_nonce, client_nonce_len, if (base16_decode(client_nonce, client_nonce_len,
cp, client_nonce_encoded_len) < 0) { cp, client_nonce_encoded_len)
!= (int) client_nonce_len) {
connection_write_str_to_buf("513 Invalid base16 client nonce\r\n", connection_write_str_to_buf("513 Invalid base16 client nonce\r\n",
conn); conn);
connection_mark_for_close(TO_CONN(conn)); connection_mark_for_close(TO_CONN(conn));

View File

@ -873,7 +873,7 @@ connection_dir_retry_bridges(smartlist_t *descs)
char digest[DIGEST_LEN]; char digest[DIGEST_LEN];
SMARTLIST_FOREACH(descs, const char *, cp, SMARTLIST_FOREACH(descs, const char *, cp,
{ {
if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp))<0) { if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp)) != DIGEST_LEN) {
log_warn(LD_BUG, "Malformed fingerprint in list: %s", log_warn(LD_BUG, "Malformed fingerprint in list: %s",
escaped(cp)); escaped(cp));
continue; continue;
@ -2682,7 +2682,8 @@ client_likes_consensus(networkstatus_t *v, const char *want_url)
if (want_len > DIGEST_LEN) if (want_len > DIGEST_LEN)
want_len = DIGEST_LEN; want_len = DIGEST_LEN;
if (base16_decode(want_digest, DIGEST_LEN, d, want_len*2) < 0) { if (base16_decode(want_digest, DIGEST_LEN, d, want_len*2)
!= (int) want_len) {
log_fn(LOG_PROTOCOL_WARN, LD_DIR, log_fn(LOG_PROTOCOL_WARN, LD_DIR,
"Failed to decode requested authority digest %s.", escaped(d)); "Failed to decode requested authority digest %s.", escaped(d));
continue; continue;
@ -3990,7 +3991,7 @@ dir_routerdesc_download_failed(smartlist_t *failed, int status_code,
} }
SMARTLIST_FOREACH_BEGIN(failed, const char *, cp) { SMARTLIST_FOREACH_BEGIN(failed, const char *, cp) {
download_status_t *dls = NULL; download_status_t *dls = NULL;
if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp)) < 0) { if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp)) != DIGEST_LEN) {
log_warn(LD_BUG, "Malformed fingerprint in list: %s", escaped(cp)); log_warn(LD_BUG, "Malformed fingerprint in list: %s", escaped(cp));
continue; continue;
} }
@ -4087,9 +4088,10 @@ dir_split_resource_into_fingerprint_pairs(const char *res,
"Skipping digest pair %s with missing dash.", escaped(cp)); "Skipping digest pair %s with missing dash.", escaped(cp));
} else { } else {
fp_pair_t pair; fp_pair_t pair;
if (base16_decode(pair.first, DIGEST_LEN, cp, HEX_DIGEST_LEN)<0 || if (base16_decode(pair.first, DIGEST_LEN,
base16_decode(pair.second, cp, HEX_DIGEST_LEN) != DIGEST_LEN ||
DIGEST_LEN, cp+HEX_DIGEST_LEN+1, HEX_DIGEST_LEN)<0) { base16_decode(pair.second,DIGEST_LEN,
cp+HEX_DIGEST_LEN+1, HEX_DIGEST_LEN) != DIGEST_LEN) {
log_info(LD_DIR, "Skipping non-decodable digest pair %s", escaped(cp)); log_info(LD_DIR, "Skipping non-decodable digest pair %s", escaped(cp));
} else { } else {
smartlist_add(pairs_result, tor_memdup(&pair, sizeof(pair))); smartlist_add(pairs_result, tor_memdup(&pair, sizeof(pair)));
@ -4167,8 +4169,9 @@ dir_split_resource_into_fingerprints(const char *resource,
} }
d = tor_malloc_zero(digest_len); d = tor_malloc_zero(digest_len);
if (decode_hex ? if (decode_hex ?
(base16_decode(d, digest_len, cp, hex_digest_len)<0) : (base16_decode(d, digest_len, cp, hex_digest_len) != digest_len) :
(base64_decode(d, digest_len, cp, base64_digest_len)<0)) { (base64_decode(d, digest_len, cp, base64_digest_len)
!= digest_len)) {
log_info(LD_DIR, "Skipping non-decodable digest %s", escaped(cp)); log_info(LD_DIR, "Skipping non-decodable digest %s", escaped(cp));
smartlist_del_keeporder(fp_tmp, i--); smartlist_del_keeporder(fp_tmp, i--);
goto again; goto again;

View File

@ -122,7 +122,8 @@ add_fingerprint_to_dir(const char *fp, authdir_config_t *list,
fingerprint = tor_strdup(fp); fingerprint = tor_strdup(fp);
tor_strstrip(fingerprint, " "); tor_strstrip(fingerprint, " ");
if (base16_decode(d, DIGEST_LEN, fingerprint, strlen(fingerprint))) { if (base16_decode(d, DIGEST_LEN,
fingerprint, strlen(fingerprint)) != DIGEST_LEN) {
log_warn(LD_DIRSERV, "Couldn't decode fingerprint \"%s\"", log_warn(LD_DIRSERV, "Couldn't decode fingerprint \"%s\"",
escaped(fp)); escaped(fp));
tor_free(fingerprint); tor_free(fingerprint);
@ -199,7 +200,7 @@ dirserv_load_fingerprint_file(void)
tor_strstrip(fingerprint, " "); /* remove spaces */ tor_strstrip(fingerprint, " "); /* remove spaces */
if (strlen(fingerprint) != HEX_DIGEST_LEN || if (strlen(fingerprint) != HEX_DIGEST_LEN ||
base16_decode(digest_tmp, sizeof(digest_tmp), base16_decode(digest_tmp, sizeof(digest_tmp),
fingerprint, HEX_DIGEST_LEN) < 0) { fingerprint, HEX_DIGEST_LEN) != sizeof(digest_tmp)) {
log_notice(LD_CONFIG, log_notice(LD_CONFIG,
"Invalid fingerprint (nickname '%s', " "Invalid fingerprint (nickname '%s', "
"fingerprint %s). Skipping.", "fingerprint %s). Skipping.",
@ -2274,7 +2275,8 @@ guardfraction_file_parse_guard_line(const char *guard_line,
inputs_tmp = smartlist_get(sl, 0); inputs_tmp = smartlist_get(sl, 0);
if (strlen(inputs_tmp) != HEX_DIGEST_LEN || if (strlen(inputs_tmp) != HEX_DIGEST_LEN ||
base16_decode(guard_id, DIGEST_LEN, inputs_tmp, HEX_DIGEST_LEN)) { base16_decode(guard_id, DIGEST_LEN,
inputs_tmp, HEX_DIGEST_LEN) != DIGEST_LEN) {
tor_asprintf(err_msg, "bad digest '%s'", inputs_tmp); tor_asprintf(err_msg, "bad digest '%s'", inputs_tmp);
goto done; goto done;
} }
@ -2578,7 +2580,8 @@ measured_bw_line_parse(measured_bw_line_t *out, const char *orig_line)
cp+=strlen("node_id=$"); cp+=strlen("node_id=$");
if (strlen(cp) != HEX_DIGEST_LEN || if (strlen(cp) != HEX_DIGEST_LEN ||
base16_decode(out->node_id, DIGEST_LEN, cp, HEX_DIGEST_LEN)) { base16_decode(out->node_id, DIGEST_LEN,
cp, HEX_DIGEST_LEN) != DIGEST_LEN) {
log_warn(LD_DIRSERV, "Invalid node_id in bandwidth file line: %s", log_warn(LD_DIRSERV, "Invalid node_id in bandwidth file line: %s",
escaped(orig_line)); escaped(orig_line));
tor_free(line); tor_free(line);

View File

@ -1253,7 +1253,7 @@ entry_guards_parse_state(or_state_t *state, int set, char **msg)
} else { } else {
strlcpy(node->nickname, smartlist_get(args,0), MAX_NICKNAME_LEN+1); strlcpy(node->nickname, smartlist_get(args,0), MAX_NICKNAME_LEN+1);
if (base16_decode(node->identity, DIGEST_LEN, smartlist_get(args,1), if (base16_decode(node->identity, DIGEST_LEN, smartlist_get(args,1),
strlen(smartlist_get(args,1)))<0) { strlen(smartlist_get(args,1))) != DIGEST_LEN) {
*msg = tor_strdup("Unable to parse entry nodes: " *msg = tor_strdup("Unable to parse entry nodes: "
"Bad hex digest for EntryGuard"); "Bad hex digest for EntryGuard");
} }
@ -1309,7 +1309,8 @@ entry_guards_parse_state(or_state_t *state, int set, char **msg)
log_warn(LD_BUG, "EntryGuardAddedBy line is not long enough."); log_warn(LD_BUG, "EntryGuardAddedBy line is not long enough.");
continue; continue;
} }
if (base16_decode(d, sizeof(d), line->value, HEX_DIGEST_LEN)<0 || if (base16_decode(d, sizeof(d),
line->value, HEX_DIGEST_LEN) != sizeof(d) ||
line->value[HEX_DIGEST_LEN] != ' ') { line->value[HEX_DIGEST_LEN] != ' ') {
log_warn(LD_BUG, "EntryGuardAddedBy line %s does not begin with " log_warn(LD_BUG, "EntryGuardAddedBy line %s does not begin with "
"hex digest", escaped(line->value)); "hex digest", escaped(line->value));

View File

@ -2216,7 +2216,7 @@ getinfo_helper_networkstatus(control_connection_t *conn,
if (*q == '$') if (*q == '$')
++q; ++q;
if (base16_decode(d, DIGEST_LEN, q, strlen(q))) { if (base16_decode(d, DIGEST_LEN, q, strlen(q)) != DIGEST_LEN) {
*errmsg = "Data not decodeable as hex"; *errmsg = "Data not decodeable as hex";
return -1; return -1;
} }

View File

@ -1074,7 +1074,8 @@ rep_hist_load_mtbf_data(time_t now)
if (mtbf_idx > i) if (mtbf_idx > i)
i = mtbf_idx; i = mtbf_idx;
} }
if (base16_decode(digest, DIGEST_LEN, hexbuf, HEX_DIGEST_LEN) < 0) { if (base16_decode(digest, DIGEST_LEN,
hexbuf, HEX_DIGEST_LEN) != DIGEST_LEN) {
log_warn(LD_HIST, "Couldn't hex string %s", escaped(hexbuf)); log_warn(LD_HIST, "Couldn't hex string %s", escaped(hexbuf));
continue; continue;
} }

View File

@ -2685,7 +2685,8 @@ hex_digest_nickname_decode(const char *hexdigest,
return -1; return -1;
} }
if (base16_decode(digest_out, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0) if (base16_decode(digest_out, DIGEST_LEN,
hexdigest, HEX_DIGEST_LEN) != DIGEST_LEN)
return -1; return -1;
return 0; return 0;
} }
@ -2770,7 +2771,7 @@ hexdigest_to_digest(const char *hexdigest, char *digest)
if (hexdigest[0]=='$') if (hexdigest[0]=='$')
++hexdigest; ++hexdigest;
if (strlen(hexdigest) < HEX_DIGEST_LEN || if (strlen(hexdigest) < HEX_DIGEST_LEN ||
base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0) base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) != DIGEST_LEN)
return -1; return -1;
return 0; return 0;
} }

View File

@ -1513,7 +1513,8 @@ router_parse_entry_from_string(const char *s, const char *end,
char d[DIGEST_LEN]; char d[DIGEST_LEN];
tor_assert(tok->n_args == 1); tor_assert(tok->n_args == 1);
tor_strstrip(tok->args[0], " "); tor_strstrip(tok->args[0], " ");
if (base16_decode(d, DIGEST_LEN, tok->args[0], strlen(tok->args[0]))) { if (base16_decode(d, DIGEST_LEN,
tok->args[0], strlen(tok->args[0])) != DIGEST_LEN) {
log_warn(LD_DIR, "Couldn't decode router fingerprint %s", log_warn(LD_DIR, "Couldn't decode router fingerprint %s",
escaped(tok->args[0])); escaped(tok->args[0]));
goto err; goto err;
@ -1594,8 +1595,10 @@ router_parse_entry_from_string(const char *s, const char *end,
if ((tok = find_opt_by_keyword(tokens, K_EXTRA_INFO_DIGEST))) { if ((tok = find_opt_by_keyword(tokens, K_EXTRA_INFO_DIGEST))) {
tor_assert(tok->n_args >= 1); tor_assert(tok->n_args >= 1);
if (strlen(tok->args[0]) == HEX_DIGEST_LEN) { if (strlen(tok->args[0]) == HEX_DIGEST_LEN) {
base16_decode(router->cache_info.extra_info_digest, if (base16_decode(router->cache_info.extra_info_digest, DIGEST_LEN,
DIGEST_LEN, tok->args[0], HEX_DIGEST_LEN); tok->args[0], HEX_DIGEST_LEN) != DIGEST_LEN) {
log_warn(LD_DIR,"Invalid extra info digest");
}
} else { } else {
log_warn(LD_DIR, "Invalid extra info digest %s", escaped(tok->args[0])); log_warn(LD_DIR, "Invalid extra info digest %s", escaped(tok->args[0]));
} }
@ -1738,7 +1741,7 @@ extrainfo_parse_entry_from_string(const char *s, const char *end,
strlcpy(extrainfo->nickname, tok->args[0], sizeof(extrainfo->nickname)); strlcpy(extrainfo->nickname, tok->args[0], sizeof(extrainfo->nickname));
if (strlen(tok->args[1]) != HEX_DIGEST_LEN || if (strlen(tok->args[1]) != HEX_DIGEST_LEN ||
base16_decode(extrainfo->cache_info.identity_digest, DIGEST_LEN, base16_decode(extrainfo->cache_info.identity_digest, DIGEST_LEN,
tok->args[1], HEX_DIGEST_LEN)) { tok->args[1], HEX_DIGEST_LEN) != DIGEST_LEN) {
log_warn(LD_DIR,"Invalid fingerprint %s on \"extra-info\"", log_warn(LD_DIR,"Invalid fingerprint %s on \"extra-info\"",
escaped(tok->args[1])); escaped(tok->args[1]));
goto err; goto err;
@ -1960,7 +1963,7 @@ authority_cert_parse_from_string(const char *s, const char **end_of_string)
tok = find_by_keyword(tokens, K_FINGERPRINT); tok = find_by_keyword(tokens, K_FINGERPRINT);
tor_assert(tok->n_args); tor_assert(tok->n_args);
if (base16_decode(fp_declared, DIGEST_LEN, tok->args[0], if (base16_decode(fp_declared, DIGEST_LEN, tok->args[0],
strlen(tok->args[0]))) { strlen(tok->args[0])) != DIGEST_LEN) {
log_warn(LD_DIR, "Couldn't decode key certificate fingerprint %s", log_warn(LD_DIR, "Couldn't decode key certificate fingerprint %s",
escaped(tok->args[0])); escaped(tok->args[0]));
goto err; goto err;
@ -3097,7 +3100,8 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
voter->nickname = tor_strdup(tok->args[0]); voter->nickname = tor_strdup(tok->args[0]);
if (strlen(tok->args[1]) != HEX_DIGEST_LEN || if (strlen(tok->args[1]) != HEX_DIGEST_LEN ||
base16_decode(voter->identity_digest, sizeof(voter->identity_digest), base16_decode(voter->identity_digest, sizeof(voter->identity_digest),
tok->args[1], HEX_DIGEST_LEN) < 0) { tok->args[1], HEX_DIGEST_LEN)
!= sizeof(voter->identity_digest)) {
log_warn(LD_DIR, "Error decoding identity digest %s in " log_warn(LD_DIR, "Error decoding identity digest %s in "
"network-status document.", escaped(tok->args[1])); "network-status document.", escaped(tok->args[1]));
goto err; goto err;
@ -3146,7 +3150,8 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
} }
if (strlen(tok->args[0]) != HEX_DIGEST_LEN || if (strlen(tok->args[0]) != HEX_DIGEST_LEN ||
base16_decode(voter->vote_digest, sizeof(voter->vote_digest), base16_decode(voter->vote_digest, sizeof(voter->vote_digest),
tok->args[0], HEX_DIGEST_LEN) < 0) { tok->args[0], HEX_DIGEST_LEN)
!= sizeof(voter->vote_digest)) {
log_warn(LD_DIR, "Error decoding vote digest %s in " log_warn(LD_DIR, "Error decoding vote digest %s in "
"network-status consensus.", escaped(tok->args[0])); "network-status consensus.", escaped(tok->args[0]));
goto err; goto err;
@ -3171,7 +3176,7 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
if (strlen(tok->args[0]) == HEX_DIGEST_LEN) { if (strlen(tok->args[0]) == HEX_DIGEST_LEN) {
networkstatus_voter_info_t *voter = smartlist_get(ns->voters, 0); networkstatus_voter_info_t *voter = smartlist_get(ns->voters, 0);
if (base16_decode(voter->legacy_id_digest, DIGEST_LEN, if (base16_decode(voter->legacy_id_digest, DIGEST_LEN,
tok->args[0], HEX_DIGEST_LEN)<0) tok->args[0], HEX_DIGEST_LEN) != DIGEST_LEN)
bad = 1; bad = 1;
else else
bad = 0; bad = 0;
@ -3330,7 +3335,8 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
if (strlen(id_hexdigest) != HEX_DIGEST_LEN || if (strlen(id_hexdigest) != HEX_DIGEST_LEN ||
base16_decode(declared_identity, sizeof(declared_identity), base16_decode(declared_identity, sizeof(declared_identity),
id_hexdigest, HEX_DIGEST_LEN) < 0) { id_hexdigest, HEX_DIGEST_LEN)
!= sizeof(declared_identity)) {
log_warn(LD_DIR, "Error decoding declared identity %s in " log_warn(LD_DIR, "Error decoding declared identity %s in "
"network-status document.", escaped(id_hexdigest)); "network-status document.", escaped(id_hexdigest));
goto err; goto err;
@ -3345,7 +3351,8 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
sig->alg = alg; sig->alg = alg;
if (strlen(sk_hexdigest) != HEX_DIGEST_LEN || if (strlen(sk_hexdigest) != HEX_DIGEST_LEN ||
base16_decode(sig->signing_key_digest, sizeof(sig->signing_key_digest), base16_decode(sig->signing_key_digest, sizeof(sig->signing_key_digest),
sk_hexdigest, HEX_DIGEST_LEN) < 0) { sk_hexdigest, HEX_DIGEST_LEN)
!= sizeof(sig->signing_key_digest)) {
log_warn(LD_DIR, "Error decoding declared signing key digest %s in " log_warn(LD_DIR, "Error decoding declared signing key digest %s in "
"network-status document.", escaped(sk_hexdigest)); "network-status document.", escaped(sk_hexdigest));
tor_free(sig); tor_free(sig);
@ -3547,7 +3554,7 @@ networkstatus_parse_detached_signatures(const char *s, const char *eos)
continue; continue;
} }
if (base16_decode(digests->d[alg], digest_length, if (base16_decode(digests->d[alg], digest_length,
hexdigest, strlen(hexdigest)) < 0) { hexdigest, strlen(hexdigest)) != (int) digest_length) {
log_warn(LD_DIR, "Bad encoding on consensus-digest in detached " log_warn(LD_DIR, "Bad encoding on consensus-digest in detached "
"networkstatus signatures"); "networkstatus signatures");
goto err; goto err;
@ -3620,14 +3627,14 @@ networkstatus_parse_detached_signatures(const char *s, const char *eos)
if (strlen(id_hexdigest) != HEX_DIGEST_LEN || if (strlen(id_hexdigest) != HEX_DIGEST_LEN ||
base16_decode(id_digest, sizeof(id_digest), base16_decode(id_digest, sizeof(id_digest),
id_hexdigest, HEX_DIGEST_LEN) < 0) { id_hexdigest, HEX_DIGEST_LEN) != sizeof(id_digest)) {
log_warn(LD_DIR, "Error decoding declared identity %s in " log_warn(LD_DIR, "Error decoding declared identity %s in "
"network-status vote.", escaped(id_hexdigest)); "network-status vote.", escaped(id_hexdigest));
goto err; goto err;
} }
if (strlen(sk_hexdigest) != HEX_DIGEST_LEN || if (strlen(sk_hexdigest) != HEX_DIGEST_LEN ||
base16_decode(sk_digest, sizeof(sk_digest), base16_decode(sk_digest, sizeof(sk_digest),
sk_hexdigest, HEX_DIGEST_LEN) < 0) { sk_hexdigest, HEX_DIGEST_LEN) != sizeof(sk_digest)) {
log_warn(LD_DIR, "Error decoding declared signing key digest %s in " log_warn(LD_DIR, "Error decoding declared signing key digest %s in "
"network-status vote.", escaped(sk_hexdigest)); "network-status vote.", escaped(sk_hexdigest));
goto err; goto err;
@ -4829,7 +4836,7 @@ tor_version_parse(const char *s, tor_version_t *out)
memwipe(digest, 0, sizeof(digest)); memwipe(digest, 0, sizeof(digest));
if ( hexlen == 0 || (hexlen % 2) == 1) if ( hexlen == 0 || (hexlen % 2) == 1)
return -1; return -1;
if (base16_decode(digest, hexlen/2, cp, hexlen)) if (base16_decode(digest, hexlen/2, cp, hexlen) != hexlen/2)
return -1; return -1;
memcpy(out->git_tag, digest, hexlen/2); memcpy(out->git_tag, digest, hexlen/2);
out->git_tag_len = hexlen/2; out->git_tag_len = hexlen/2;

View File

@ -1521,7 +1521,7 @@ test_crypto_formats(void *arg)
strlcpy(data1, "f0d678affc000100", 1024); strlcpy(data1, "f0d678affc000100", 1024);
i = base16_decode(data2, 8, data1, 16); i = base16_decode(data2, 8, data1, 16);
tt_int_op(i,OP_EQ, 0); tt_int_op(i,OP_EQ, 8);
tt_mem_op(data2,OP_EQ, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8); tt_mem_op(data2,OP_EQ, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
/* now try some failing base16 decodes */ /* now try some failing base16 decodes */
@ -2537,8 +2537,9 @@ test_crypto_ed25519_testvectors(void *arg)
#define DECODE(p,s) base16_decode((char*)(p),sizeof(p),(s),strlen(s)) #define DECODE(p,s) base16_decode((char*)(p),sizeof(p),(s),strlen(s))
#define EQ(a,h) test_memeq_hex((const char*)(a), (h)) #define EQ(a,h) test_memeq_hex((const char*)(a), (h))
tt_int_op(0, OP_EQ, DECODE(sk, ED25519_SECRET_KEYS[i])); tt_int_op(sizeof(sk), OP_EQ, DECODE(sk, ED25519_SECRET_KEYS[i]));
tt_int_op(0, OP_EQ, DECODE(blinding_param, ED25519_BLINDING_PARAMS[i])); tt_int_op(sizeof(blinding_param), OP_EQ, DECODE(blinding_param,
ED25519_BLINDING_PARAMS[i]));
tt_int_op(0, OP_EQ, ed25519_secret_key_from_seed(&esk, sk)); tt_int_op(0, OP_EQ, ed25519_secret_key_from_seed(&esk, sk));
EQ(esk.seckey, ED25519_EXPANDED_SECRET_KEYS[i]); EQ(esk.seckey, ED25519_EXPANDED_SECRET_KEYS[i]);

View File

@ -580,7 +580,7 @@ test_dir_extrainfo_parsing(void *arg)
crypto_pk_t *pk = ri->identity_pkey = crypto_pk_new(); \ crypto_pk_t *pk = ri->identity_pkey = crypto_pk_new(); \
tt_assert(! crypto_pk_read_public_key_from_string(pk, \ tt_assert(! crypto_pk_read_public_key_from_string(pk, \
name##_KEY, strlen(name##_KEY))); \ name##_KEY, strlen(name##_KEY))); \
tt_int_op(0,OP_EQ,base16_decode(d, 20, name##_FP, strlen(name##_FP))); \ tt_int_op(20,OP_EQ,base16_decode(d, 20, name##_FP, strlen(name##_FP))); \
digestmap_set((digestmap_t*)map, d, ri); \ digestmap_set((digestmap_t*)map, d, ri); \
ri = NULL; \ ri = NULL; \
} while (0) } while (0)

View File

@ -40,7 +40,7 @@ gen_vote_routerstatus_for_tests(const char *digest_in_hex, int is_guard)
tt_int_op(strlen(digest_in_hex), ==, HEX_DIGEST_LEN); tt_int_op(strlen(digest_in_hex), ==, HEX_DIGEST_LEN);
retval = base16_decode(digest_tmp, sizeof(digest_tmp), retval = base16_decode(digest_tmp, sizeof(digest_tmp),
digest_in_hex, HEX_DIGEST_LEN); digest_in_hex, HEX_DIGEST_LEN);
tt_int_op(retval, ==, 0); tt_int_op(retval, ==, sizeof(digest_tmp));
memcpy(rs->identity_digest, digest_tmp, DIGEST_LEN); memcpy(rs->identity_digest, digest_tmp, DIGEST_LEN);
} }

View File

@ -783,7 +783,8 @@ test_md_reject_cache(void *arg)
mc = get_microdesc_cache(); mc = get_microdesc_cache();
#define ADD(hex) \ #define ADD(hex) \
do { \ do { \
tt_int_op(0,OP_EQ,base16_decode(buf,sizeof(buf),hex,strlen(hex))); \ tt_int_op(sizeof(buf),OP_EQ,base16_decode(buf,sizeof(buf), \
hex,strlen(hex)));\
smartlist_add(wanted, tor_memdup(buf, DIGEST256_LEN)); \ smartlist_add(wanted, tor_memdup(buf, DIGEST256_LEN)); \
} while (0) } while (0)

View File

@ -21,7 +21,7 @@
} STMT_END } STMT_END
#define BASE16(idx, var, n) STMT_BEGIN { \ #define BASE16(idx, var, n) STMT_BEGIN { \
const char *s = argv[(idx)]; \ const char *s = argv[(idx)]; \
if (base16_decode((char*)var, n, s, strlen(s)) < 0 ) { \ if (base16_decode((char*)var, n, s, strlen(s)) < (int)n ) { \
fprintf(stderr, "couldn't decode argument %d (%s)\n",idx,s); \ fprintf(stderr, "couldn't decode argument %d (%s)\n",idx,s); \
return 1; \ return 1; \
} \ } \

View File

@ -270,7 +270,7 @@ test_util_format_base16_decode(void *ignored)
tt_int_op(res, OP_EQ, 0); tt_int_op(res, OP_EQ, 0);
res = base16_decode(dst, 1000, "aabc", 4); res = base16_decode(dst, 1000, "aabc", 4);
tt_int_op(res, OP_EQ, 0); tt_int_op(res, OP_EQ, 2);
tt_mem_op(dst, OP_EQ, "\xaa\xbc", 2); tt_mem_op(dst, OP_EQ, "\xaa\xbc", 2);
res = base16_decode(dst, 1000, "aabcd", 6); res = base16_decode(dst, 1000, "aabcd", 6);
@ -280,7 +280,7 @@ test_util_format_base16_decode(void *ignored)
tt_int_op(res, OP_EQ, -1); tt_int_op(res, OP_EQ, -1);
res = base16_decode(real_dst, 10, real_src, 14); res = base16_decode(real_dst, 10, real_src, 14);
tt_int_op(res, OP_EQ, 0); tt_int_op(res, OP_EQ, 7);
tt_mem_op(real_dst, OP_EQ, expected, 7); tt_mem_op(real_dst, OP_EQ, expected, 7);
done: done: