add negative test for put/get DHT feature. fix bug in signature verification and in DHT unit test
This commit is contained in:
parent
1ca493b20d
commit
a627a4e156
|
@ -961,12 +961,12 @@ void node_impl::incoming_request(msg const& m, entry& e)
|
||||||
// mutable put, we must verify the signature
|
// mutable put, we must verify the signature
|
||||||
// generate the message digest by merging the sequence number and the
|
// generate the message digest by merging the sequence number and the
|
||||||
|
|
||||||
char seq[1020];
|
char seq[1100];
|
||||||
int len = snprintf(seq, sizeof(seq), "3:seqi%" PRId64 "e1:v", msg_keys[2]->int_value());
|
int len = snprintf(seq, sizeof(seq), "3:seqi%" PRId64 "e1:v", msg_keys[2]->int_value());
|
||||||
std::pair<char const*, int> buf = msg_keys[1]->data_section();
|
std::pair<char const*, int> buf = msg_keys[1]->data_section();
|
||||||
memcpy(seq + len, buf.first, buf.second);
|
memcpy(seq + len, buf.first, buf.second);
|
||||||
len += buf.second;
|
len += buf.second;
|
||||||
TORRENT_ASSERT(len <= 1020);
|
TORRENT_ASSERT(len <= 1100);
|
||||||
|
|
||||||
#ifdef TORRENT_USE_VALGRIND
|
#ifdef TORRENT_USE_VALGRIND
|
||||||
VALGRIND_CHECK_MEM_IS_DEFINED(buf.first, buf.second);
|
VALGRIND_CHECK_MEM_IS_DEFINED(buf.first, buf.second);
|
||||||
|
@ -977,7 +977,7 @@ void node_impl::incoming_request(msg const& m, entry& e)
|
||||||
// msg_keys[4] is the signature, msg_keys[3] is the public key
|
// msg_keys[4] is the signature, msg_keys[3] is the public key
|
||||||
if (ed25519_verify((unsigned char const*)msg_keys[4]->string_ptr()
|
if (ed25519_verify((unsigned char const*)msg_keys[4]->string_ptr()
|
||||||
, (unsigned char const*)seq, len
|
, (unsigned char const*)seq, len
|
||||||
, (unsigned char const*)msg_keys[3]->string_ptr()) != 0)
|
, (unsigned char const*)msg_keys[3]->string_ptr()) != 1)
|
||||||
{
|
{
|
||||||
incoming_error(e, "invalid signature", 206);
|
incoming_error(e, "invalid signature", 206);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -683,7 +683,8 @@ int test_main()
|
||||||
int pos = snprintf(buffer, sizeof(buffer), "3:seqi%de1:v", seq);
|
int pos = snprintf(buffer, sizeof(buffer), "3:seqi%de1:v", seq);
|
||||||
char* ptr = buffer + pos;
|
char* ptr = buffer + pos;
|
||||||
pos += bencode(ptr, items[0].ent);
|
pos += bencode(ptr, items[0].ent);
|
||||||
ed25519_sign(signature, (unsigned char*)buffer, pos, private_key, public_key);
|
ed25519_sign(signature, (unsigned char*)buffer, pos, public_key, private_key);
|
||||||
|
TEST_EQUAL(ed25519_verify(signature, (unsigned char*)buffer, pos, public_key), 1);
|
||||||
#ifdef TORRENT_USE_VALGRIND
|
#ifdef TORRENT_USE_VALGRIND
|
||||||
VALGRIND_CHECK_MEM_IS_DEFINED(signature, 64);
|
VALGRIND_CHECK_MEM_IS_DEFINED(signature, 64);
|
||||||
#endif
|
#endif
|
||||||
|
@ -747,6 +748,47 @@ int test_main()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// also test that invalid signatures fail!
|
||||||
|
|
||||||
|
pos = snprintf(buffer, sizeof(buffer), "3:seqi%de1:v", seq);
|
||||||
|
ptr = buffer + pos;
|
||||||
|
pos += bencode(ptr, items[0].ent);
|
||||||
|
ed25519_sign(signature, (unsigned char*)buffer, pos, public_key, private_key);
|
||||||
|
TEST_EQUAL(ed25519_verify(signature, (unsigned char*)buffer, pos, public_key), 1);
|
||||||
|
#ifdef TORRENT_USE_VALGRIND
|
||||||
|
VALGRIND_CHECK_MEM_IS_DEFINED(signature, 64);
|
||||||
|
#endif
|
||||||
|
// break the signature
|
||||||
|
signature[2] ^= 0xaa;
|
||||||
|
|
||||||
|
TEST_CHECK(ed25519_verify(signature, (unsigned char*)buffer, pos, public_key) != 1);
|
||||||
|
|
||||||
|
send_dht_msg(node, "put", source, &response, "10", 0
|
||||||
|
, 0, token, 0, 0, &items[0].ent, false, false
|
||||||
|
, std::string((char*)public_key, 32)
|
||||||
|
, std::string((char*)signature, 64), seq);
|
||||||
|
|
||||||
|
key_desc_t desc_error[] =
|
||||||
|
{
|
||||||
|
{ "e", lazy_entry::list_t, 2, 0 },
|
||||||
|
{ "y", lazy_entry::string_t, 1, 0},
|
||||||
|
};
|
||||||
|
|
||||||
|
ret = verify_message(&response, desc_error, parsed, 2, error_string, sizeof(error_string));
|
||||||
|
if (ret)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "put response: %s\n", print_entry(response).c_str());
|
||||||
|
TEST_EQUAL(parsed[1]->string_value(), "e");
|
||||||
|
// 206 is the code for invalid signature
|
||||||
|
TEST_EQUAL(parsed[0]->list_int_value_at(0), 206);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, " invalid put response: %s\n%s\n"
|
||||||
|
, error_string, print_entry(response).c_str());
|
||||||
|
TEST_ERROR(error_string);
|
||||||
|
}
|
||||||
|
|
||||||
// === test CAS put ===
|
// === test CAS put ===
|
||||||
|
|
||||||
// this is the hash that we expect to be there
|
// this is the hash that we expect to be there
|
||||||
|
@ -757,7 +799,8 @@ int test_main()
|
||||||
ptr = buffer + pos;
|
ptr = buffer + pos;
|
||||||
// put item 1
|
// put item 1
|
||||||
pos += bencode(ptr, items[1].ent);
|
pos += bencode(ptr, items[1].ent);
|
||||||
ed25519_sign(signature, (unsigned char*)buffer, pos, private_key, public_key);
|
ed25519_sign(signature, (unsigned char*)buffer, pos, public_key, private_key);
|
||||||
|
TEST_EQUAL(ed25519_verify(signature, (unsigned char*)buffer, pos, public_key), 1);
|
||||||
#ifdef TORRENT_USE_VALGRIND
|
#ifdef TORRENT_USE_VALGRIND
|
||||||
VALGRIND_CHECK_MEM_IS_DEFINED(signature, 64);
|
VALGRIND_CHECK_MEM_IS_DEFINED(signature, 64);
|
||||||
#endif
|
#endif
|
||||||
|
@ -791,14 +834,7 @@ int test_main()
|
||||||
, std::string((char*)signature, 64), seq
|
, std::string((char*)signature, 64), seq
|
||||||
, (char const*)&cas[0]);
|
, (char const*)&cas[0]);
|
||||||
|
|
||||||
|
ret = verify_message(&response, desc_error, parsed, 2, error_string, sizeof(error_string));
|
||||||
key_desc_t desc4[] =
|
|
||||||
{
|
|
||||||
{ "e", lazy_entry::list_t, 2, 0 },
|
|
||||||
{ "y", lazy_entry::string_t, 1, 0},
|
|
||||||
};
|
|
||||||
|
|
||||||
ret = verify_message(&response, desc4, parsed, 2, error_string, sizeof(error_string));
|
|
||||||
if (ret)
|
if (ret)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "put response: %s\n"
|
fprintf(stderr, "put response: %s\n"
|
||||||
|
|
Loading…
Reference in New Issue