implement piece verification using old signatures. now i can reenable banning peers due to bad pieces.

This commit is contained in:
Miguel Freitas 2013-11-06 09:22:05 -02:00
parent 8ec4b3e16e
commit 2c256b6edb
4 changed files with 21 additions and 48 deletions

14
TODO
View File

@ -1,17 +1,3 @@
- Take care of posts using older public key when key is replaced.
notes: not very difficult, GetTransaction must receive a maximum block number to search the
transaction (we get this from post["height"]). another txIndex should be set to speedup lookup
(key in db includes the number of the block that changed tx so previous one can be found).
pseudocode:
getTxIndex( key = "userX" ) => block h contains this tx;
while( h > max_h )
getTxIndex( "userX_h" ) => block h contains the previous tx
=> GetTransation: new parameter maxHeight done!
- Until old public key is properly used, disable banning torrent peers due to bad piece hashes.
note: torrent.cpp line 3286 (function piece_failed), iteration to ban peers is disabled (continue).
- Count UTF8 chars in acceptSignedPost to proper limit the 140 characters. - Count UTF8 chars in acceptSignedPost to proper limit the 140 characters.
- Encrypt user_data (which contains all DMs) - Encrypt user_data (which contains all DMs)

View File

@ -3308,9 +3308,6 @@ namespace libtorrent
for (std::set<void*>::iterator i = peers.begin() for (std::set<void*>::iterator i = peers.begin()
, end(peers.end()); i != end; ++i) , end(peers.end()); i != end; ++i)
{ {
// [MF] FIXME FIXME: BANNING BY FAILED HASH DISABLED - READ TODO!
continue;
policy::peer* p = static_cast<policy::peer*>(*i); policy::peer* p = static_cast<policy::peer*>(*i);
if (p == 0) continue; if (p == 0) continue;
TORRENT_ASSERT(p->in_use); TORRENT_ASSERT(p->in_use);

View File

@ -606,44 +606,33 @@ std::string createSignature(std::string const &strMessage, std::string const &st
} }
bool getUserPubKey(std::string const &strUsername, CPubKey &pubkey) bool getUserPubKey(std::string const &strUsername, CPubKey &pubkey, int maxHeight)
{ {
{ CTransaction txOut;
CKeyID keyID; uint256 hashBlock;
if( pwalletMain->GetKeyIdFromUsername(strUsername, keyID) ) { if( !GetTransaction(strUsername, txOut, hashBlock, maxHeight) ) {
if( !pwalletMain->GetPubKey(keyID, pubkey) ) { //printf("getUserPubKey: user unknown '%s'\n", strUsername.c_str());
// error? should not have failed. return false;
}
}
} }
std::vector< std::vector<unsigned char> > vData;
if( !txOut.pubKey.ExtractPushData(vData) || vData.size() < 1 ) {
printf("getUserPubKey: broken pubkey for user '%s'\n", strUsername.c_str());
return false;
}
pubkey = CPubKey(vData[0]);
if( !pubkey.IsValid() ) { if( !pubkey.IsValid() ) {
CTransaction txOut; printf("getUserPubKey: invalid pubkey for user '%s'\n", strUsername.c_str());
uint256 hashBlock; return false;
if( !GetTransaction(strUsername, txOut, hashBlock) ) {
//printf("getUserPubKey: user unknown '%s'\n", strUsername.c_str());
return false;
}
std::vector< std::vector<unsigned char> > vData;
if( !txOut.pubKey.ExtractPushData(vData) || vData.size() < 1 ) {
printf("getUserPubKey: broken pubkey for user '%s'\n", strUsername.c_str());
return false;
}
pubkey = CPubKey(vData[0]);
if( !pubkey.IsValid() ) {
printf("getUserPubKey: invalid pubkey for user '%s'\n", strUsername.c_str());
return false;
}
} }
return true; return true;
} }
bool verifySignature(std::string const &strMessage, std::string const &strUsername, std::string const &strSign) bool verifySignature(std::string const &strMessage, std::string const &strUsername, std::string const &strSign, int maxHeight)
{ {
CPubKey pubkey; CPubKey pubkey;
if( !getUserPubKey(strUsername, pubkey) ) { if( !getUserPubKey(strUsername, pubkey, maxHeight) ) {
printf("verifySignature: no pubkey for user '%s'\n", strUsername.c_str()); printf("verifySignature: no pubkey for user '%s'\n", strUsername.c_str());
return false; return false;
} }
@ -761,7 +750,7 @@ bool acceptSignedPost(char const *data, int data_size, std::string username, int
std::pair<char const*, int> postbuf = post->data_section(); std::pair<char const*, int> postbuf = post->data_section();
ret = verifySignature( ret = verifySignature(
std::string(postbuf.first,postbuf.second), std::string(postbuf.first,postbuf.second),
username, sig); username, sig, height);
if( !ret ) { if( !ret ) {
sprintf(errbuf,"bad post signature"); sprintf(errbuf,"bad post signature");
} else { } else {
@ -771,11 +760,12 @@ bool acceptSignedPost(char const *data, int data_size, std::string username, int
if( rt ) { if( rt ) {
if( flags ) (*flags) |= USERPOST_FLAG_RT; if( flags ) (*flags) |= USERPOST_FLAG_RT;
std::string username_rt = rt->dict_find_string_value("n"); std::string username_rt = rt->dict_find_string_value("n");
int height_rt = rt->dict_find_int_value("height",-1);
std::pair<char const*, int> rtbuf = rt->data_section(); std::pair<char const*, int> rtbuf = rt->data_section();
ret = verifySignature( ret = verifySignature(
std::string(rtbuf.first,rtbuf.second), std::string(rtbuf.first,rtbuf.second),
username_rt, sig_rt); username_rt, sig_rt, height_rt);
if( !ret ) { if( !ret ) {
sprintf(errbuf,"bad RT signature"); sprintf(errbuf,"bad RT signature");
} }

View File

@ -23,10 +23,10 @@ public:
void startSessionTorrent(boost::thread_group& threadGroup); void startSessionTorrent(boost::thread_group& threadGroup);
void stopSessionTorrent(); void stopSessionTorrent();
bool getUserPubKey(std::string const &strUsername, CPubKey &pubkey); bool getUserPubKey(std::string const &strUsername, CPubKey &pubkey, int maxHeight = -1);
std::string createSignature(std::string const &strMessage, CKeyID &keyID); std::string createSignature(std::string const &strMessage, CKeyID &keyID);
std::string createSignature(std::string const &strMessage, std::string const &strUsername); std::string createSignature(std::string const &strMessage, std::string const &strUsername);
bool verifySignature(std::string const &strMessage, std::string const &strUsername, std::string const &strSign); bool verifySignature(std::string const &strMessage, std::string const &strUsername, std::string const &strSign, int maxHeight = -1);
bool acceptSignedPost(char const *data, int data_size, std::string username, int seq, std::string &errmsg, boost::uint32_t *flags); bool acceptSignedPost(char const *data, int data_size, std::string username, int seq, std::string &errmsg, boost::uint32_t *flags);
bool validatePostNumberForUser(std::string const &username, int k); bool validatePostNumberForUser(std::string const &username, int k);