#define CONSDIFF_PRIVATE
#include "core/or/or.h"
#include "feature/dircommon/consdiff.h"
#include "lib/memarea/memarea.h"
#include "feature/dirparse/ns_parse.h"
static const char* ns_diff_version = "network-status-diff-version 1";
static const char* hash_token = "hash";
static char *consensus_join_lines(const smartlist_t *inp);
STATIC int
lines_eq(const cdline_t *a, const cdline_t *b)
{
return a->len == b->len && fast_memeq(a->s, b->s, a->len);
}
STATIC int
line_str_eq(const cdline_t *a, const char *b)
{
const size_t len = strlen(b);
tor_assert(len <= UINT32_MAX);
cdline_t bline = { b, (uint32_t)len };
return lines_eq(a, &bline);
}
static int
line_starts_with_str(const cdline_t *a, const char *b)
{
const size_t len = strlen(b);
tor_assert(len <= UINT32_MAX);
return a->len >= len && fast_memeq(a->s, b, len);
}
static cdline_t *
cdline_linecpy(memarea_t *area, const char *s)
{
size_t len = strlen(s);
const char *ss = memarea_memdup(area, s, len);
cdline_t *line = memarea_alloc(area, sizeof(cdline_t));
line->s = ss;
line->len = (uint32_t)len;
return line;
}
STATIC void
smartlist_add_linecpy(smartlist_t *lst, memarea_t *area, const char *s)
{
smartlist_add(lst, cdline_linecpy(area, s));
}
MOCK_IMPL(STATIC int,
consensus_compute_digest,(const char *cons, size_t len,
consensus_digest_t *digest_out))
{
int r = crypto_digest256((char*)digest_out->sha3_256,
cons, len, DIGEST_SHA3_256);
return r;
}
MOCK_IMPL(STATIC int,
consensus_compute_digest_as_signed,(const char *cons, size_t len,
consensus_digest_t *digest_out))
{
return router_get_networkstatus_v3_sha3_as_signed(digest_out->sha3_256,
cons, len);
}
MOCK_IMPL(STATIC int,
consensus_digest_eq,(const uint8_t *d1,
const uint8_t *d2))
{
return fast_memeq(d1, d2, DIGEST256_LEN);
}
STATIC smartlist_slice_t *
smartlist_slice(const smartlist_t *list, int start, int end)
{
int list_len = smartlist_len(list);
tor_assert(start >= 0);
tor_assert(start <= list_len);
if (end == -1) {
end = list_len;
}
tor_assert(start <= end);
smartlist_slice_t *slice = tor_malloc(sizeof(smartlist_slice_t));
slice->list = list;
slice->offset = start;
slice->len = end - start;
return slice;
}
STATIC int *
lcs_lengths(const smartlist_slice_t *slice1, const smartlist_slice_t *slice2,
int direction)
{
size_t a_size = sizeof(int) * (slice2->len+1);
int *result = tor_malloc_zero(a_size);
int *prev = tor_malloc(a_size);
tor_assert(direction == 1 || direction == -1);
int si = slice1->offset;
if (direction == -1) {
si += (slice1->len-1);
}
for (int i = 0; i < slice1->len; ++i, si+=direction) {
const cdline_t *line1 = smartlist_get(slice1->list, si);
memcpy(prev, result, a_size);
int sj = slice2->offset;
if (direction == -1) {
sj += (slice2->len-1);
}
for (int j = 0; j < slice2->len; ++j, sj+=direction) {
const cdline_t *line2 = smartlist_get(slice2->list, sj);
if (lines_eq(line1, line2)) {
result[j + 1] = prev[j] + 1;
} else {
result[j + 1] = MAX(result[j], prev[j + 1]);
}
}
}
tor_free(prev);
return result;
}
STATIC void
trim_slices(smartlist_slice_t *slice1, smartlist_slice_t *slice2)
{
while (slice1->len>0 && slice2->len>0) {
const cdline_t *line1 = smartlist_get(slice1->list, slice1->offset);
const cdline_t *line2 = smartlist_get(slice2->list, slice2->offset);
if (!lines_eq(line1, line2)) {
break;
}
slice1->offset++; slice1->len--;
slice2->offset++; slice2->len--;
}
int i1 = (slice1->offset+slice1->len)-1;
int i2 = (slice2->offset+slice2->len)-1;
while (slice1->len>0 && slice2->len>0) {
const cdline_t *line1 = smartlist_get(slice1->list, i1);
const cdline_t *line2 = smartlist_get(slice2->list, i2);
if (!lines_eq(line1, line2)) {
break;
}
i1--;
slice1->len--;
i2--;
slice2->len--;
}
}
STATIC int
smartlist_slice_string_pos(const smartlist_slice_t *slice,
const cdline_t *string)
{
int end = slice->offset + slice->len;
for (int i = slice->offset; i < end; ++i) {
const cdline_t *el = smartlist_get(slice->list, i);
if (lines_eq(el, string)) {
return i;
}
}
return -1;
}
STATIC void
set_changed(bitarray_t *changed1, bitarray_t *changed2,
const smartlist_slice_t *slice1, const smartlist_slice_t *slice2)
{
int toskip = -1;
tor_assert(slice1->len == 0 || slice1->len == 1);
if (slice1->len == 1) {
const cdline_t *line_common = smartlist_get(slice1->list, slice1->offset);
toskip = smartlist_slice_string_pos(slice2, line_common);
if (toskip == -1) {
bitarray_set(changed1, slice1->offset);
}
}
int end = slice2->offset + slice2->len;
for (int i = slice2->offset; i < end; ++i) {
if (i != toskip) {
bitarray_set(changed2, i);
}
}
}
static int
optimal_column_to_split(const smartlist_slice_t *top,
const smartlist_slice_t *bot,
const smartlist_slice_t *slice2)
{
int *lens_top = lcs_lengths(top, slice2, 1);
int *lens_bot = lcs_lengths(bot, slice2, -1);
int column=0, max_sum=-1;
for (int i = 0; i < slice2->len+1; ++i) {
int sum = lens_top[i] + lens_bot[slice2->len-i];
if (sum > max_sum) {
column = i;
max_sum = sum;
}
}
tor_free(lens_top);
tor_free(lens_bot);
return column;
}
STATIC void
calc_changes(smartlist_slice_t *slice1,
smartlist_slice_t *slice2,
bitarray_t *changed1, bitarray_t *changed2)
{
trim_slices(slice1, slice2);
if (slice1->len <= 1) {
set_changed(changed1, changed2, slice1, slice2);
} else if (slice2->len <= 1) {
set_changed(changed2, changed1, slice2, slice1);
} else {
smartlist_slice_t *top, *bot, *left, *right;
int mid = slice1->len/2;
top = smartlist_slice(slice1->list, slice1->offset, slice1->offset+mid);
bot = smartlist_slice(slice1->list, slice1->offset+mid,
slice1->offset+slice1->len);
int mid2 = optimal_column_to_split(top, bot, slice2);
left = smartlist_slice(slice2->list, slice2->offset, slice2->offset+mid2);
right = smartlist_slice(slice2->list, slice2->offset+mid2,
slice2->offset+slice2->len);
calc_changes(top, left, changed1, changed2);
calc_changes(bot, right, changed1, changed2);
tor_free(top);
tor_free(bot);
tor_free(left);
tor_free(right);
}
}
#define NOT_VALID_BASE64 255
#define X NOT_VALID_BASE64
#define SP NOT_VALID_BASE64
#define PAD NOT_VALID_BASE64
static const uint8_t base64_compare_table[256] = {
X, X, X, X, X, X, X, X, X, SP, SP, SP, X, SP, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
SP, X, X, X, X, X, X, X, X, X, X, 62, X, X, X, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, X, X, X, PAD, X, X,
X, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, X, X, X, X, X,
X, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
};
STATIC int
get_id_hash(const cdline_t *line, cdline_t *hash_out)
{
if (line->len < 2)
return -1;
const char *hash = memchr(line->s + 2, ' ', line->len - 2);
if (!hash) {
return -1;
}
hash++;
const char *hash_end = hash;
while (base64_compare_table[*((unsigned char*)hash_end)]
!= NOT_VALID_BASE64 &&
hash_end < line->s + line->len) {
hash_end++;
}
if (hash_end == hash) {
return -1;
}
hash_out->s = hash;
tor_assert(hash_end >= hash);
tor_assert((size_t)(hash_end - hash) <= UINT32_MAX);
hash_out->len = (uint32_t)(hash_end - hash);
return 0;
}
STATIC int
is_valid_router_entry(const cdline_t *line)
{
if (line->len < 2 || fast_memneq(line->s, "r ", 2))
return 0;
cdline_t tmp;
return (get_id_hash(line, &tmp) == 0);
}
STATIC int
next_router(const smartlist_t *cons, int cur)
{
int len = smartlist_len(cons);
tor_assert(cur >= -1 && cur < len);
if (++cur >= len) {
return len;
}
const cdline_t *line = smartlist_get(cons, cur);
while (!is_valid_router_entry(line)) {
if (++cur >= len) {
return len;
}
line = smartlist_get(cons, cur);
}
return cur;
}
STATIC int
base64cmp(const cdline_t *hash1, const cdline_t *hash2)
{
if (!hash1->s && !hash2->s) {
return 0;
}
if (!hash1->s) {
return -1;
}
if (!hash2->s) {
return 1;
}
const unsigned char *a = (unsigned char*)hash1->s;
const unsigned char *b = (unsigned char*)hash2->s;
const unsigned char *a_end = a + hash1->len;
const unsigned char *b_end = b + hash2->len;
while (1) {
uint8_t av = base64_compare_table[*a];
uint8_t bv = base64_compare_table[*b];
if (av == NOT_VALID_BASE64) {
if (bv == NOT_VALID_BASE64) {
return 0;
} else {
return -1;
}
} else if (bv == NOT_VALID_BASE64) {
return 1;
} else if (av < bv) {
return -1;
} else if (av > bv) {
return 1;
} else {
a++;
b++;
if (a == a_end) {
if (b == b_end) {
return 0;
} else {
return -1;
}
} else if (b == b_end) {
return 1;
}
}
}
}
typedef struct router_id_iterator_t {
cdline_t last_hash;
cdline_t hash;
} router_id_iterator_t;
#ifndef COCCI
#define ROUTER_ID_ITERATOR_INIT { { NULL, 0 }, { NULL, 0 } }
#endif
static int
find_next_router_line(const smartlist_t *cons,
const char *consname,
int *idxp,
router_id_iterator_t *iter)
{
*idxp = next_router(cons, *idxp);
if (*idxp < smartlist_len(cons)) {
memcpy(&iter->last_hash, &iter->hash, sizeof(cdline_t));
if (get_id_hash(smartlist_get(cons, *idxp), &iter->hash) < 0 ||
base64cmp(&iter->hash, &iter->last_hash) <= 0) {
log_warn(LD_CONSDIFF, "Refusing to generate consensus diff because "
"the %s consensus doesn't have its router entries sorted "
"properly.", consname);
return -1;
}
}
return 0;
}
#define START_OF_SIGNATURES_SECTION "directory-signature "
static cdline_t *
preprocess_consensus(memarea_t *area,
smartlist_t *cons)
{
int idx;
int dirsig_idx = -1;
for (idx = 0; idx < smartlist_len(cons); ++idx) {
cdline_t *line = smartlist_get(cons, idx);
if (line_starts_with_str(line, START_OF_SIGNATURES_SECTION)) {
dirsig_idx = idx;
break;
}
}
if (dirsig_idx >= 0) {
char buf[64];
while (smartlist_len(cons) > dirsig_idx)
smartlist_del(cons, dirsig_idx);
tor_snprintf(buf, sizeof(buf), "%d,$d", dirsig_idx+1);
return cdline_linecpy(area, buf);
} else {
return NULL;
}
}
STATIC smartlist_t *
gen_ed_diff(const smartlist_t *cons1_orig, const smartlist_t *cons2,
memarea_t *area)
{
smartlist_t *cons1 = smartlist_new();
smartlist_add_all(cons1, cons1_orig);
cdline_t *remove_trailer = preprocess_consensus(area, cons1);
int len1 = smartlist_len(cons1);
int len2 = smartlist_len(cons2);
smartlist_t *result = smartlist_new();
if (remove_trailer) {
smartlist_add(result, remove_trailer);
}
bitarray_t *changed1 = bitarray_init_zero(len1);
bitarray_t *changed2 = bitarray_init_zero(len2);
int i1=-1, i2=-1;
int start1=0, start2=0;
router_id_iterator_t iter1 = ROUTER_ID_ITERATOR_INIT;
router_id_iterator_t iter2 = ROUTER_ID_ITERATOR_INIT;
while (i1 < len1 || i2 < len2) {
if (i1 < len1) {
if (find_next_router_line(cons1, "base", &i1, &iter1) < 0) {
goto error_cleanup;
}
}
if (i2 < len2) {
if (find_next_router_line(cons2, "target", &i2, &iter2) < 0) {
goto error_cleanup;
}
}
if (i1 < len1 || i2 < len2) {
int cmp = base64cmp(&iter1.hash, &iter2.hash);
while (cmp != 0) {
if (i1 < len1 && cmp < 0) {
if (find_next_router_line(cons1, "base", &i1, &iter1) < 0) {
goto error_cleanup;
}
if (i1 == len1) {
i2 = len2;
break;
}
} else if (i2 < len2 && cmp > 0) {
if (find_next_router_line(cons2, "target", &i2, &iter2) < 0) {
goto error_cleanup;
}
if (i2 == len2) {
i1 = len1;
break;
}
} else {
i1 = len1;
i2 = len2;
break;
}
cmp = base64cmp(&iter1.hash, &iter2.hash);
}
}
#define MAX_LINE_COUNT (10000)
if (i1-start1 > MAX_LINE_COUNT || i2-start2 > MAX_LINE_COUNT) {
log_warn(LD_CONSDIFF, "Refusing to generate consensus diff because "
"we found too few common router ids.");
goto error_cleanup;
}
smartlist_slice_t *cons1_sl = smartlist_slice(cons1, start1, i1);
smartlist_slice_t *cons2_sl = smartlist_slice(cons2, start2, i2);
calc_changes(cons1_sl, cons2_sl, changed1, changed2);
tor_free(cons1_sl);
tor_free(cons2_sl);
start1 = i1, start2 = i2;
}
i1=len1-1, i2=len2-1;
char buf[128];
while (i1 >= 0 || i2 >= 0) {
int start1x, start2x, end1, end2, added, deleted;
if (!(i1 >= 0 && bitarray_is_set(changed1, i1)) &&
!(i2 >= 0 && bitarray_is_set(changed2, i2))) {
if (i1 >= 0) {
i1--;
}
if (i2 >= 0) {
i2--;
}
continue;
}
end1 = i1, end2 = i2;
while (i1 >= 0 && bitarray_is_set(changed1, i1)) {
i1--;
}
while (i2 >= 0 && bitarray_is_set(changed2, i2)) {
i2--;
}
start1x = i1+1, start2x = i2+1;
added = end2-i2, deleted = end1-i1;
if (added == 0) {
if (deleted == 1) {
tor_snprintf(buf, sizeof(buf), "%id", start1x+1);
smartlist_add_linecpy(result, area, buf);
} else {
tor_snprintf(buf, sizeof(buf), "%i,%id", start1x+1, start1x+deleted);
smartlist_add_linecpy(result, area, buf);
}
} else {
int i;
if (deleted == 0) {
tor_snprintf(buf, sizeof(buf), "%ia", start1x);
smartlist_add_linecpy(result, area, buf);
} else if (deleted == 1) {
tor_snprintf(buf, sizeof(buf), "%ic", start1x+1);
smartlist_add_linecpy(result, area, buf);
} else {
tor_snprintf(buf, sizeof(buf), "%i,%ic", start1x+1, start1x+deleted);
smartlist_add_linecpy(result, area, buf);
}
for (i = start2x; i <= end2; ++i) {
cdline_t *line = smartlist_get(cons2, i);
if (line_str_eq(line, ".")) {
log_warn(LD_CONSDIFF, "Cannot generate consensus diff because "
"one of the lines to be added is \".\".");
goto error_cleanup;
}
smartlist_add(result, line);
}
smartlist_add_linecpy(result, area, ".");
}
}
smartlist_free(cons1);
bitarray_free(changed1);
bitarray_free(changed2);
return result;
error_cleanup:
smartlist_free(cons1);
bitarray_free(changed1);
bitarray_free(changed2);
smartlist_free(result);
return NULL;
}
static int
get_linenum(const char **s, int *num_out)
{
int ok;
char *next;
if (!TOR_ISDIGIT(**s)) {
return -1;
}
*num_out = (int) tor_parse_long(*s, 10, 0, INT32_MAX, &ok, &next);
if (ok && next) {
*s = next;
return 0;
} else {
return -1;
}
}
STATIC smartlist_t *
apply_ed_diff(const smartlist_t *cons1, const smartlist_t *diff,
int diff_starting_line)
{
int diff_len = smartlist_len(diff);
int j = smartlist_len(cons1);
smartlist_t *cons2 = smartlist_new();
for (int i=diff_starting_line; i<diff_len; ++i) {
const cdline_t *diff_cdline = smartlist_get(diff, i);
char diff_line[128];
if (diff_cdline->len > sizeof(diff_line) - 1) {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"an ed command was far too long");
goto error_cleanup;
}
memcpy(diff_line, diff_cdline->s, diff_cdline->len);
diff_line[diff_cdline->len] = 0;
const char *ptr = diff_line;
int start = 0, end = 0;
int had_range = 0;
int end_was_eof = 0;
if (get_linenum(&ptr, &start) < 0) {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"an ed command was missing a line number.");
goto error_cleanup;
}
if (*ptr == ',') {
had_range = 1;
++ptr;
if (*ptr == '$') {
end_was_eof = 1;
end = smartlist_len(cons1);
++ptr;
} else if (get_linenum(&ptr, &end) < 0) {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"an ed command was missing a range end line number.");
goto error_cleanup;
}
if (end <= start) {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"an invalid range was found in an ed command.");
goto error_cleanup;
}
} else {
end = start;
}
if (end > j) {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"its commands are not properly sorted in reverse order.");
goto error_cleanup;
}
if (*ptr == '\0') {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"a line with no ed command was found");
goto error_cleanup;
}
if (*(ptr+1) != '\0') {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"an ed command longer than one char was found.");
goto error_cleanup;
}
char action = *ptr;
switch (action) {
case 'a':
case 'c':
case 'd':
break;
default:
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"an unrecognised ed command was found.");
goto error_cleanup;
}
if (end_was_eof && action != 'd') {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"it wanted to use $ with a command other than delete");
goto error_cleanup;
}
if (had_range && action == 'a') {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"it wanted to add lines after a range.");
goto error_cleanup;
}
for (; j && j > end; --j) {
cdline_t *cons_line = smartlist_get(cons1, j-1);
smartlist_add(cons2, cons_line);
}
if (action == 'c' || action == 'd') {
while (--j >= start) {
}
}
if (action == 'a' || action == 'c') {
int added_end = i;
i++;
while (i < diff_len) {
if (line_str_eq(smartlist_get(diff, i), ".")) {
break;
}
if (++i == diff_len) {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"it has lines to be inserted that don't end with a \".\".");
goto error_cleanup;
}
}
int added_i = i-1;
if (added_i == added_end) {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"it has an ed command that tries to insert zero lines.");
goto error_cleanup;
}
while (added_i > added_end) {
cdline_t *added_line = smartlist_get(diff, added_i--);
smartlist_add(cons2, added_line);
}
}
}
for (; j > 0; --j) {
cdline_t *cons_line = smartlist_get(cons1, j-1);
smartlist_add(cons2, cons_line);
}
smartlist_reverse(cons2);
return cons2;
error_cleanup:
smartlist_free(cons2);
return NULL;
}
smartlist_t *
consdiff_gen_diff(const smartlist_t *cons1,
const smartlist_t *cons2,
const consensus_digest_t *digests1,
const consensus_digest_t *digests2,
memarea_t *area)
{
smartlist_t *ed_diff = gen_ed_diff(cons1, cons2, area);
if (!ed_diff) {
goto error_cleanup;
}
smartlist_t *ed_cons2 = apply_ed_diff(cons1, ed_diff, 0);
if (!ed_cons2) {
log_warn(LD_BUG|LD_CONSDIFF, "Refusing to generate consensus diff because "
"the generated ed diff could not be tested to successfully generate "
"the target consensus.");
goto error_cleanup;
}
int cons2_eq = 1;
if (smartlist_len(cons2) == smartlist_len(ed_cons2)) {
SMARTLIST_FOREACH_BEGIN(cons2, const cdline_t *, line1) {
const cdline_t *line2 = smartlist_get(ed_cons2, line1_sl_idx);
if (!lines_eq(line1, line2)) {
cons2_eq = 0;
break;
}
} SMARTLIST_FOREACH_END(line1);
} else {
cons2_eq = 0;
}
smartlist_free(ed_cons2);
if (!cons2_eq) {
log_warn(LD_BUG|LD_CONSDIFF, "Refusing to generate consensus diff because "
"the generated ed diff did not generate the target consensus "
"successfully when tested.");
goto error_cleanup;
}
char cons1_hash_hex[HEX_DIGEST256_LEN+1];
char cons2_hash_hex[HEX_DIGEST256_LEN+1];
base16_encode(cons1_hash_hex, HEX_DIGEST256_LEN+1,
(const char*)digests1->sha3_256, DIGEST256_LEN);
base16_encode(cons2_hash_hex, HEX_DIGEST256_LEN+1,
(const char*)digests2->sha3_256, DIGEST256_LEN);
char buf[160];
smartlist_t *result = smartlist_new();
tor_snprintf(buf, sizeof(buf), "%s", ns_diff_version);
smartlist_add_linecpy(result, area, buf);
tor_snprintf(buf, sizeof(buf), "%s %s %s", hash_token,
cons1_hash_hex, cons2_hash_hex);
smartlist_add_linecpy(result, area, buf);
smartlist_add_all(result, ed_diff);
smartlist_free(ed_diff);
return result;
error_cleanup:
if (ed_diff) {
smartlist_free(ed_diff);
}
return NULL;
}
int
consdiff_get_digests(const smartlist_t *diff,
char *digest1_out,
char *digest2_out)
{
smartlist_t *hash_words = NULL;
const cdline_t *format;
char cons1_hash[DIGEST256_LEN], cons2_hash[DIGEST256_LEN];
char *cons1_hash_hex, *cons2_hash_hex;
if (smartlist_len(diff) < 2) {
log_info(LD_CONSDIFF, "The provided consensus diff is too short.");
goto error_cleanup;
}
format = smartlist_get(diff, 0);
if (!line_str_eq(format, ns_diff_version)) {
log_warn(LD_CONSDIFF, "The provided consensus diff format is not known.");
goto error_cleanup;
}
hash_words = smartlist_new();
{
const cdline_t *line2 = smartlist_get(diff, 1);
char *h = tor_memdup_nulterm(line2->s, line2->len);
smartlist_split_string(hash_words, h, " ", 0, 0);
tor_free(h);
}
if (smartlist_len(hash_words) != 3 ||
strcmp(smartlist_get(hash_words, 0), hash_token)) {
log_info(LD_CONSDIFF, "The provided consensus diff does not include "
"the necessary digests.");
goto error_cleanup;
}
cons1_hash_hex = smartlist_get(hash_words, 1);
cons2_hash_hex = smartlist_get(hash_words, 2);
if (strlen(cons1_hash_hex) != HEX_DIGEST256_LEN ||
strlen(cons2_hash_hex) != HEX_DIGEST256_LEN) {
log_info(LD_CONSDIFF, "The provided consensus diff includes "
"base16-encoded digests of incorrect size.");
goto error_cleanup;
}
if (base16_decode(cons1_hash, DIGEST256_LEN,
cons1_hash_hex, HEX_DIGEST256_LEN) != DIGEST256_LEN ||
base16_decode(cons2_hash, DIGEST256_LEN,
cons2_hash_hex, HEX_DIGEST256_LEN) != DIGEST256_LEN) {
log_info(LD_CONSDIFF, "The provided consensus diff includes "
"malformed digests.");
goto error_cleanup;
}
if (digest1_out) {
memcpy(digest1_out, cons1_hash, DIGEST256_LEN);
}
if (digest2_out) {
memcpy(digest2_out, cons2_hash, DIGEST256_LEN);
}
SMARTLIST_FOREACH(hash_words, char *, cp, tor_free(cp));
smartlist_free(hash_words);
return 0;
error_cleanup:
if (hash_words) {
SMARTLIST_FOREACH(hash_words, char *, cp, tor_free(cp));
smartlist_free(hash_words);
}
return 1;
}
char *
consdiff_apply_diff(const smartlist_t *cons1,
const smartlist_t *diff,
const consensus_digest_t *digests1)
{
smartlist_t *cons2 = NULL;
char *cons2_str = NULL;
char e_cons1_hash[DIGEST256_LEN];
char e_cons2_hash[DIGEST256_LEN];
if (consdiff_get_digests(diff, e_cons1_hash, e_cons2_hash) != 0) {
goto error_cleanup;
}
if (!consensus_digest_eq(digests1->sha3_256,
(const uint8_t*)e_cons1_hash)) {
char hex_digest1[HEX_DIGEST256_LEN+1];
char e_hex_digest1[HEX_DIGEST256_LEN+1];
log_warn(LD_CONSDIFF, "Refusing to apply consensus diff because "
"the base consensus doesn't match the digest as found in "
"the consensus diff header.");
base16_encode(hex_digest1, HEX_DIGEST256_LEN+1,
(const char *)digests1->sha3_256, DIGEST256_LEN);
base16_encode(e_hex_digest1, HEX_DIGEST256_LEN+1,
e_cons1_hash, DIGEST256_LEN);
log_warn(LD_CONSDIFF, "Expected: %s; found: %s",
hex_digest1, e_hex_digest1);
goto error_cleanup;
}
cons2 = apply_ed_diff(cons1, diff, 2);
if (!cons2) {
goto error_cleanup;
}
cons2_str = consensus_join_lines(cons2);
consensus_digest_t cons2_digests;
if (consensus_compute_digest(cons2_str, strlen(cons2_str),
&cons2_digests) < 0) {
log_warn(LD_CONSDIFF, "Could not compute digests of the consensus "
"resulting from applying a consensus diff.");
goto error_cleanup;
}
if (!consensus_digest_eq(cons2_digests.sha3_256,
(const uint8_t*)e_cons2_hash)) {
log_warn(LD_CONSDIFF, "Refusing to apply consensus diff because "
"the resulting consensus doesn't match the digest as found in "
"the consensus diff header.");
char hex_digest2[HEX_DIGEST256_LEN+1];
char e_hex_digest2[HEX_DIGEST256_LEN+1];
base16_encode(hex_digest2, HEX_DIGEST256_LEN+1,
(const char *)cons2_digests.sha3_256, DIGEST256_LEN);
base16_encode(e_hex_digest2, HEX_DIGEST256_LEN+1,
e_cons2_hash, DIGEST256_LEN);
log_warn(LD_CONSDIFF, "Expected: %s; found: %s",
hex_digest2, e_hex_digest2);
goto error_cleanup;
}
goto done;
error_cleanup:
tor_free(cons2_str);
done:
if (cons2) {
smartlist_free(cons2);
}
return cons2_str;
}
#define CONSENSUS_LINE_MAX_LEN (1<<20)
STATIC int
consensus_split_lines(smartlist_t *out,
const char *s, size_t len,
memarea_t *area)
{
const char *end_of_str = s + len;
while (s < end_of_str) {
const char *eol = memchr(s, '\n', end_of_str - s);
if (!eol) {
return -1;
}
if (eol - s > CONSENSUS_LINE_MAX_LEN) {
return -1;
}
cdline_t *line = memarea_alloc(area, sizeof(cdline_t));
line->s = s;
line->len = (uint32_t)(eol - s);
smartlist_add(out, line);
s = eol+1;
}
return 0;
}
static char *
consensus_join_lines(const smartlist_t *inp)
{
size_t n = 0;
SMARTLIST_FOREACH(inp, const cdline_t *, cdline, n += cdline->len + 1);
n += 1;
char *result = tor_malloc(n);
char *out = result;
SMARTLIST_FOREACH_BEGIN(inp, const cdline_t *, cdline) {
memcpy(out, cdline->s, cdline->len);
out += cdline->len;
*out++ = '\n';
} SMARTLIST_FOREACH_END(cdline);
*out++ = '\0';
tor_assert(out == result+n);
return result;
}
char *
consensus_diff_generate(const char *cons1, size_t cons1len,
const char *cons2, size_t cons2len)
{
consensus_digest_t d1, d2;
smartlist_t *lines1 = NULL, *lines2 = NULL, *result_lines = NULL;
int r1, r2;
char *result = NULL;
r1 = consensus_compute_digest_as_signed(cons1, cons1len, &d1);
r2 = consensus_compute_digest(cons2, cons2len, &d2);
if (BUG(r1 < 0 || r2 < 0))
return NULL;
memarea_t *area = memarea_new();
lines1 = smartlist_new();
lines2 = smartlist_new();
if (consensus_split_lines(lines1, cons1, cons1len, area) < 0)
goto done;
if (consensus_split_lines(lines2, cons2, cons2len, area) < 0)
goto done;
result_lines = consdiff_gen_diff(lines1, lines2, &d1, &d2, area);
done:
if (result_lines) {
result = consensus_join_lines(result_lines);
smartlist_free(result_lines);
}
memarea_drop_all(area);
smartlist_free(lines1);
smartlist_free(lines2);
return result;
}
char *
consensus_diff_apply(const char *consensus,
size_t consensus_len,
const char *diff,
size_t diff_len)
{
consensus_digest_t d1;
smartlist_t *lines1 = NULL, *lines2 = NULL;
int r1;
char *result = NULL;
memarea_t *area = memarea_new();
r1 = consensus_compute_digest_as_signed(consensus, consensus_len, &d1);
if (BUG(r1 < 0))
goto done;
lines1 = smartlist_new();
lines2 = smartlist_new();
if (consensus_split_lines(lines1, consensus, consensus_len, area) < 0)
goto done;
if (consensus_split_lines(lines2, diff, diff_len, area) < 0)
goto done;
result = consdiff_apply_diff(lines1, lines2, &d1);
done:
smartlist_free(lines1);
smartlist_free(lines2);
memarea_drop_all(area);
return result;
}
int
looks_like_a_consensus_diff(const char *document, size_t len)
{
return (len >= strlen(ns_diff_version) &&
fast_memeq(document, ns_diff_version, strlen(ns_diff_version)));
}