Skip to content

Commit

Permalink
merge #858: backport #855: Fix bad mn sig ipv6
Browse files Browse the repository at this point in the history
51696ac make use of getnameinfo() optional (default: true)
bec39b8 fix mnb sig bug for ipv6 addresses: use pure byte to hex conversion for construction of masternode broadcast signature, better logging
  • Loading branch information
UdjinM6 authored and schinzelh committed Jun 2, 2016
1 parent 9567524 commit 00540c8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
10 changes: 7 additions & 3 deletions src/darksend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2110,10 +2110,14 @@ bool CDarkSendSigner::VerifyMessage(CPubKey pubkey, vector<unsigned char>& vchSi
return false;
}

if (fDebug && pubkey2.GetID() != pubkey.GetID())
LogPrintf("CDarkSendSigner::VerifyMessage -- keys don't match: %s %s\n", pubkey2.GetID().ToString(), pubkey.GetID().ToString());
if (pubkey2.GetID() != pubkey.GetID()) {
errorMessage = strprintf("keys don't match - input: %s, recovered: %s, message: %s, sig: %s\n",
pubkey.GetID().ToString(), pubkey2.GetID().ToString(), strMessage,
EncodeBase64(&vchSig[0], vchSig.size()));
return false;
}

return (pubkey2.GetID() == pubkey.GetID());
return true;
}

bool CDarksendQueue::Sign()
Expand Down
9 changes: 5 additions & 4 deletions src/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,17 @@ bool CMasternodeBroadcast::CheckAndUpdate(int& nDos)
if(protocolVersion < 70201) {
std::string vchPubKey(pubkey.begin(), pubkey.end());
std::string vchPubKey2(pubkey2.begin(), pubkey2.end());
strMessage = addr.ToString() + boost::lexical_cast<std::string>(sigTime) + vchPubKey + vchPubKey2 + boost::lexical_cast<std::string>(protocolVersion);
strMessage = addr.ToString(false) + boost::lexical_cast<std::string>(sigTime) + vchPubKey + vchPubKey2 + boost::lexical_cast<std::string>(protocolVersion);
} else {
strMessage = addr.ToString() + boost::lexical_cast<std::string>(sigTime) +
strMessage = addr.ToString(false) + boost::lexical_cast<std::string>(sigTime) +
pubkey.GetID().ToString() + pubkey2.GetID().ToString() +
boost::lexical_cast<std::string>(protocolVersion);
}

std::string errorMessage = "";
LogPrint("masternode", "mnb - strMessage: %s, pubkey address: %s, sig: %s\n", strMessage, CBitcoinAddress(pubkey.GetID()).ToString(), EncodeBase64(&sig[0], sig.size()));
if(!darkSendSigner.VerifyMessage(pubkey, sig, strMessage, errorMessage)){
LogPrintf("mnb - Got bad Masternode address signature\n");
LogPrintf("mnb - Got bad Masternode address signature, error: %s\n", errorMessage);
// There is a bug in old MN signatures, ignore such MN but do not ban the peer we got this from
nDos = protocolVersion < 70201 ? 0 : 100;
return false;
Expand Down Expand Up @@ -527,7 +528,7 @@ bool CMasternodeBroadcast::Sign(CKey& keyCollateralAddress)

sigTime = GetAdjustedTime();

std::string strMessage = addr.ToString() + boost::lexical_cast<std::string>(sigTime) + vchPubKey + vchPubKey2 + boost::lexical_cast<std::string>(protocolVersion);
std::string strMessage = addr.ToString(false) + boost::lexical_cast<std::string>(sigTime) + vchPubKey + vchPubKey2 + boost::lexical_cast<std::string>(protocolVersion);

if(!darkSendSigner.SignMessage(strMessage, errorMessage, sig, keyCollateralAddress)) {
LogPrintf("CMasternodeBroadcast::Sign() - Error: %s\n", errorMessage);
Expand Down
29 changes: 16 additions & 13 deletions src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,17 +827,20 @@ enum Network CNetAddr::GetNetwork() const
return NET_IPV6;
}

std::string CNetAddr::ToStringIP() const
std::string CNetAddr::ToStringIP(bool fUseGetnameinfo) const
{
if (IsTor())
return EncodeBase32(&ip[6], 10) + ".onion";
CService serv(*this, 0);
struct sockaddr_storage sockaddr;
socklen_t socklen = sizeof(sockaddr);
if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
char name[1025] = "";
if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name, sizeof(name), NULL, 0, NI_NUMERICHOST))
return std::string(name);
if (fUseGetnameinfo)
{
CService serv(*this, 0);
struct sockaddr_storage sockaddr;
socklen_t socklen = sizeof(sockaddr);
if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
char name[1025] = "";
if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name, sizeof(name), NULL, 0, NI_NUMERICHOST))
return std::string(name);
}
}
if (IsIPv4())
return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
Expand Down Expand Up @@ -1174,18 +1177,18 @@ std::string CService::ToStringPort() const
return strprintf("%u", port);
}

std::string CService::ToStringIPPort() const
std::string CService::ToStringIPPort(bool fUseGetnameinfo) const
{
if (IsIPv4() || IsTor()) {
return ToStringIP() + ":" + ToStringPort();
return ToStringIP(fUseGetnameinfo) + ":" + ToStringPort();
} else {
return "[" + ToStringIP() + "]:" + ToStringPort();
return "[" + ToStringIP(fUseGetnameinfo) + "]:" + ToStringPort();
}
}

std::string CService::ToString() const
std::string CService::ToString(bool fUseGetnameinfo) const
{
return ToStringIPPort();
return ToStringIPPort(fUseGetnameinfo);
}

void CService::SetPort(unsigned short portIn)
Expand Down
6 changes: 3 additions & 3 deletions src/netbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class CNetAddr
bool IsMulticast() const;
enum Network GetNetwork() const;
std::string ToString() const;
std::string ToStringIP() const;
std::string ToStringIP(bool fUseGetnameinfo = true) const;
unsigned int GetByte(int n) const;
uint64_t GetHash() const;
bool GetInAddr(struct in_addr* pipv4Addr) const;
Expand Down Expand Up @@ -151,9 +151,9 @@ class CService : public CNetAddr
friend bool operator!=(const CService& a, const CService& b);
friend bool operator<(const CService& a, const CService& b);
std::vector<unsigned char> GetKey() const;
std::string ToString() const;
std::string ToString(bool fUseGetnameinfo = true) const;
std::string ToStringPort() const;
std::string ToStringIPPort() const;
std::string ToStringIPPort(bool fUseGetnameinfo = true) const;

CService(const struct in6_addr& ipv6Addr, unsigned short port);
CService(const struct sockaddr_in6& addr);
Expand Down

0 comments on commit 00540c8

Please sign in to comment.