fix non-openssl builds and improved error messages for mutable puts

This commit is contained in:
Arvid Norberg 2011-05-25 02:41:48 +00:00
parent 6fa1827c39
commit 2bb53ce6e8
3 changed files with 18 additions and 12 deletions

View File

@ -792,25 +792,22 @@ void node_impl::incoming_request(msg const& m, entry& e)
const static key_desc_t msg_desc[] = {
{"token", lazy_entry::string_t, 0, 0},
{"v", lazy_entry::none_t, 0, 0},
{"seq", lazy_entry::int_t, 0, 0},
{"seq", lazy_entry::int_t, 0, key_desc_t::optional},
// public key
{"k", lazy_entry::string_t, 268, 0},
{"sig", lazy_entry::string_t, 256, 0},
{"k", lazy_entry::string_t, 268, key_desc_t::optional},
{"sig", lazy_entry::string_t, 256, key_desc_t::optional},
};
// attempt to parse the message
lazy_entry const* msg_keys[5];
if (!verify_message(arg_ent, msg_desc, msg_keys, 2, error_string, sizeof(error_string)))
if (!verify_message(arg_ent, msg_desc, msg_keys, 5, error_string, sizeof(error_string)))
{
incoming_error(e, error_string);
return;
}
bool mutable_put = false;
// is this a mutable put?
if (verify_message(arg_ent, msg_desc, msg_keys, 5, error_string, sizeof(error_string)))
mutable_put = true;
bool mutable_put = (msg_keys[2] && msg_keys[3] && msg_keys[4]);
// pointer and length to the whole entry
std::pair<char const*, int> buf = msg_keys[1]->data_section();
@ -882,12 +879,17 @@ void node_impl::incoming_request(msg const& m, entry& e)
std::pair<char const*, int> buf = msg_keys[1]->data_section();
digest.update(buf.first, buf.second);
#ifdef TORRENT_USE_OPENSSL
if (!verify_rsa(digest.final(), msg_keys[3]->string_ptr(), msg_keys[3]->string_length()
, msg_keys[4]->string_ptr(), msg_keys[4]->string_length()))
{
incoming_error(e, "invalid signature");
return;
}
#else
incoming_error(e, "unsupported");
return;
#endif
rsa_key target;
memcpy(target.bytes, msg_keys[3]->string_ptr(), sizeof(target.bytes));

View File

@ -119,7 +119,7 @@ namespace libtorrent
{
// returns the size of the resulting signature
int sign_rsa(char const* data, int data_len
int sign_rsa(sha1_hash const& digest
, char const* private_key, int private_len
, char* signature, int sig_len)
{
@ -127,7 +127,7 @@ int sign_rsa(char const* data, int data_len
}
// returns true if the signature is valid
bool verify_rsa(char const* data, int data_len
bool verify_rsa(sha1_hash const& digest
, char const* public_key, int public_len
, char const* signature, int sig_len)
{

View File

@ -428,8 +428,8 @@ int test_main()
fprintf(stderr, "seeds: %f\n", seeds.size());
fprintf(stderr, "downloaders: %f\n", downloaders.size());
TEST_CHECK(fabs(seeds.size() - 50.f) <= 2.f);
TEST_CHECK(fabs(downloaders.size() - 50.f) <= 2.f);
TEST_CHECK(fabs(seeds.size() - 50.f) <= 3.f);
TEST_CHECK(fabs(downloaders.size() - 50.f) <= 3.f);
}
else
{
@ -490,6 +490,9 @@ int test_main()
announce_immutable_items(node, eps, items, sizeof(items)/sizeof(items[0]));
#ifdef TORRENT_USE_OPENSSL
// RSA functions are only implemented with openssl for now
// ==== get / put mutable items ===
char private_key[1192];
@ -562,6 +565,7 @@ int test_main()
, error_string, print_entry(response).c_str());
TEST_ERROR(error_string);
}
#endif // TORRENT_USE_OPENSSL
return 0;
}