Starting to add sha-1 and aes wrapper.
This commit is contained in:
@@ -42,39 +42,38 @@ fromHex(int ch)
|
|||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HashBuf::~HashBuf()
|
||||||
MD5Buf::MD5Buf()
|
|
||||||
{
|
{
|
||||||
memset(data, 0, sizeof(data));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
MD5Buf::ToString() const
|
HashBuf::ToString() const
|
||||||
{
|
{
|
||||||
// Create a hex-based string from the MD5 data.
|
// Create a hex-based string from the MD5 data.
|
||||||
string retValue;
|
string retValue;
|
||||||
char tmpBuf[2 + 1];
|
char tmpBuf[2 + 1];
|
||||||
tmpBuf[sizeof(tmpBuf) - 1] = 0;
|
tmpBuf[sizeof(tmpBuf) - 1] = 0;
|
||||||
for (int i = 0; i < MD5_DATA_SIZE; i++)
|
const unsigned char *tmpData = GetData();
|
||||||
|
for (int i = 0; i < GetDataSize(); i++)
|
||||||
{
|
{
|
||||||
sprintf(tmpBuf, "%02x", data[i]);
|
sprintf(tmpBuf, "%02x", tmpData[i]);
|
||||||
retValue += tmpBuf;
|
retValue += tmpBuf;
|
||||||
}
|
}
|
||||||
return retValue;
|
return retValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
MD5Buf::FromString(const std::string &text)
|
HashBuf::FromString(const std::string &text)
|
||||||
{
|
{
|
||||||
// Convert hex-based string to MD5 data.
|
// Convert hex-based string to MD5 data.
|
||||||
bool retVal = false;
|
bool retVal = false;
|
||||||
if (text.size() == 2 * MD5_DATA_SIZE)
|
int tmpSize = GetDataSize();
|
||||||
|
if (text.size() == 2 * (unsigned)tmpSize)
|
||||||
{
|
{
|
||||||
unsigned char *tmpData = data;
|
unsigned char *tmpData = GetData();
|
||||||
const char *t = text.c_str();
|
const char *t = text.c_str();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (; i < MD5_DATA_SIZE; i++) {
|
for (; i < tmpSize; i++) {
|
||||||
int part1 = fromHex(*t++);
|
int part1 = fromHex(*t++);
|
||||||
if (part1 == -1)
|
if (part1 == -1)
|
||||||
break;
|
break;
|
||||||
@@ -83,27 +82,83 @@ MD5Buf::FromString(const std::string &text)
|
|||||||
break;
|
break;
|
||||||
*tmpData++ = (part1<<4) + part2;
|
*tmpData++ = (part1<<4) + part2;
|
||||||
}
|
}
|
||||||
retVal = i == MD5_DATA_SIZE;
|
retVal = i == tmpSize;
|
||||||
}
|
}
|
||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
MD5Buf::IsZero() const
|
HashBuf::IsZero() const
|
||||||
{
|
{
|
||||||
return *this == MD5Buf();
|
int dataSize = GetDataSize();
|
||||||
|
const unsigned char *tmpData = GetData();
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < dataSize; i++)
|
||||||
|
{
|
||||||
|
if (tmpData[i] != 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return i == dataSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
MD5Buf::operator==(const MD5Buf &other) const
|
HashBuf::operator==(const HashBuf &other) const
|
||||||
{
|
{
|
||||||
return memcmp(data, other.data, MD5_DATA_SIZE) == 0;
|
return GetDataSize() == other.GetDataSize() && memcmp(GetData(), other.GetData(), GetDataSize()) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
MD5Buf::operator<(const MD5Buf &other) const
|
HashBuf::operator<(const HashBuf &other) const
|
||||||
{
|
{
|
||||||
return memcmp(data, other.data, MD5_DATA_SIZE) < 0;
|
int smallestDataSize = GetDataSize() < other.GetDataSize() ? GetDataSize() : other.GetDataSize();
|
||||||
|
return memcmp(GetData(), other.GetData(), smallestDataSize) < 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
MD5Buf::MD5Buf()
|
||||||
|
{
|
||||||
|
memset(m_data, 0, sizeof(m_data));
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char *
|
||||||
|
MD5Buf::GetData()
|
||||||
|
{
|
||||||
|
return m_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
const unsigned char *
|
||||||
|
MD5Buf::GetData() const
|
||||||
|
{
|
||||||
|
return m_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
MD5Buf::GetDataSize() const
|
||||||
|
{
|
||||||
|
return sizeof(m_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
SHA1Buf::SHA1Buf()
|
||||||
|
{
|
||||||
|
memset(m_data, 0, sizeof(m_data));
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char *
|
||||||
|
SHA1Buf::GetData()
|
||||||
|
{
|
||||||
|
return m_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
const unsigned char *
|
||||||
|
SHA1Buf::GetData() const
|
||||||
|
{
|
||||||
|
return m_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
SHA1Buf::GetDataSize() const
|
||||||
|
{
|
||||||
|
return sizeof(m_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@@ -122,7 +177,7 @@ CryptHelper::MD5Sum(const std::string &fileName, MD5Buf &buf)
|
|||||||
MD5_Init(&context);
|
MD5_Init(&context);
|
||||||
while ((numBytes = fread(readBuf, 1, sizeof(readBuf), file)) > 0)
|
while ((numBytes = fread(readBuf, 1, sizeof(readBuf), file)) > 0)
|
||||||
MD5_Update(&context, readBuf, numBytes);
|
MD5_Update(&context, readBuf, numBytes);
|
||||||
MD5_Final(buf.data, &context);
|
MD5_Final(buf.GetData(), &context);
|
||||||
retVal = ferror(file) == 0;
|
retVal = ferror(file) == 0;
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
@@ -130,3 +185,24 @@ CryptHelper::MD5Sum(const std::string &fileName, MD5Buf &buf)
|
|||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
CryptHelper::SHA1Hash(unsigned char *data, unsigned dataSize, SHA1Buf &buf)
|
||||||
|
{
|
||||||
|
bool retVal = false;
|
||||||
|
#ifdef HAVE_OPENSSL
|
||||||
|
if (SHA1(data, dataSize, buf.GetData()) != NULL)
|
||||||
|
retVal = true;
|
||||||
|
#else
|
||||||
|
// TODO
|
||||||
|
#endif
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
CryptHelper::AES128Encrypt(unsigned char *keyData, unsigned keySize, unsigned char *plainData, unsigned plainSize, std::vector<unsigned char> &outCipher)
|
||||||
|
{
|
||||||
|
bool retVal = false;
|
||||||
|
//#ifdef HAVE_OPENSSL
|
||||||
|
// int errCode = EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(), NULL, keyData, keySize,
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|||||||
+37
-5
@@ -22,21 +22,51 @@
|
|||||||
#define _CRYPTHELPER_H_
|
#define _CRYPTHELPER_H_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#define MD5_DATA_SIZE 16
|
#define MD5_DATA_SIZE 16
|
||||||
|
#define SHA1_DATA_SIZE 20
|
||||||
|
|
||||||
struct MD5Buf
|
class HashBuf
|
||||||
{
|
{
|
||||||
MD5Buf();
|
public:
|
||||||
|
virtual ~HashBuf();
|
||||||
|
|
||||||
std::string ToString() const;
|
std::string ToString() const;
|
||||||
bool FromString(const std::string &text);
|
bool FromString(const std::string &text);
|
||||||
bool IsZero() const;
|
bool IsZero() const;
|
||||||
|
|
||||||
bool operator==(const MD5Buf &other) const;
|
bool operator==(const HashBuf &other) const;
|
||||||
bool operator<(const MD5Buf &other) const;
|
bool operator<(const HashBuf &other) const;
|
||||||
|
|
||||||
unsigned char data[MD5_DATA_SIZE];
|
virtual unsigned char *GetData() = 0;
|
||||||
|
virtual const unsigned char *GetData() const = 0;
|
||||||
|
virtual int GetDataSize() const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class MD5Buf : public HashBuf
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MD5Buf();
|
||||||
|
|
||||||
|
virtual unsigned char *GetData();
|
||||||
|
virtual const unsigned char *GetData() const;
|
||||||
|
virtual int GetDataSize() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
unsigned char m_data[MD5_DATA_SIZE];
|
||||||
|
};
|
||||||
|
|
||||||
|
class SHA1Buf : public HashBuf
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SHA1Buf();
|
||||||
|
|
||||||
|
virtual unsigned char *GetData();
|
||||||
|
virtual const unsigned char *GetData() const;
|
||||||
|
virtual int GetDataSize() const;
|
||||||
|
|
||||||
|
unsigned char m_data[SHA1_DATA_SIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
class CryptHelper
|
class CryptHelper
|
||||||
@@ -44,6 +74,8 @@ class CryptHelper
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
static bool MD5Sum(const std::string &fileName, MD5Buf &buf);
|
static bool MD5Sum(const std::string &fileName, MD5Buf &buf);
|
||||||
|
static bool SHA1Hash(unsigned char *data, unsigned dataSize, SHA1Buf &buf);
|
||||||
|
static bool AES128Encrypt(unsigned char *keyData, unsigned keySize, unsigned char *plainData, unsigned plainSize, std::vector<unsigned char> &outCipher);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -53,7 +53,9 @@
|
|||||||
#ifdef HAVE_OPENSSL
|
#ifdef HAVE_OPENSSL
|
||||||
#include <openssl/ssl.h>
|
#include <openssl/ssl.h>
|
||||||
#include <openssl/md5.h>
|
#include <openssl/md5.h>
|
||||||
|
#include <openssl/sha.h>
|
||||||
#include <openssl/rand.h>
|
#include <openssl/rand.h>
|
||||||
|
#include <openssl/evp.h>
|
||||||
#else
|
#else
|
||||||
// For all other systems, we use GnuTLS.
|
// For all other systems, we use GnuTLS.
|
||||||
#include <gnutls/openssl.h>
|
#include <gnutls/openssl.h>
|
||||||
|
|||||||
@@ -675,7 +675,7 @@ AbstractClientStateReceiving::HandlePacket(boost::shared_ptr<ClientThread> clien
|
|||||||
if (netInfo->avatarData != NULL)
|
if (netInfo->avatarData != NULL)
|
||||||
{
|
{
|
||||||
tmpInfo.hasAvatar = true;
|
tmpInfo.hasAvatar = true;
|
||||||
memcpy(tmpInfo.avatar.data, netInfo->avatarData->avatar.buf, MD5_DATA_SIZE);
|
memcpy(tmpInfo.avatar.GetData(), netInfo->avatarData->avatar.buf, MD5_DATA_SIZE);
|
||||||
tmpInfo.avatarType = (AvatarFileType)netInfo->avatarData->avatarType;
|
tmpInfo.avatarType = (AvatarFileType)netInfo->avatarData->avatarType;
|
||||||
}
|
}
|
||||||
client->SetPlayerInfo(
|
client->SetPlayerInfo(
|
||||||
@@ -1025,7 +1025,7 @@ ClientStateStartSession::InternalHandlePacket(boost::shared_ptr<ClientThread> cl
|
|||||||
noauthLogin->avatar =
|
noauthLogin->avatar =
|
||||||
OCTET_STRING_new_fromBuf(
|
OCTET_STRING_new_fromBuf(
|
||||||
&asn_DEF_OCTET_STRING,
|
&asn_DEF_OCTET_STRING,
|
||||||
(const char *)tmpMD5.data,
|
(const char *)tmpMD5.GetData(),
|
||||||
MD5_DATA_SIZE);
|
MD5_DATA_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1124,7 +1124,7 @@ ClientStateWaitEnterLogin::TimerLoop(const boost::system::error_code& ec, boost:
|
|||||||
authLogin->avatar =
|
authLogin->avatar =
|
||||||
OCTET_STRING_new_fromBuf(
|
OCTET_STRING_new_fromBuf(
|
||||||
&asn_DEF_OCTET_STRING,
|
&asn_DEF_OCTET_STRING,
|
||||||
(const char *)tmpMD5.data,
|
(const char *)tmpMD5.GetData(),
|
||||||
MD5_DATA_SIZE);
|
MD5_DATA_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -704,7 +704,7 @@ ClientThread::RetrieveAvatarIfNeeded(unsigned id, const PlayerInfo &info)
|
|||||||
AvatarRequestMessage_t *netAvatar = &packet->GetMsg()->choice.avatarRequestMessage;
|
AvatarRequestMessage_t *netAvatar = &packet->GetMsg()->choice.avatarRequestMessage;
|
||||||
netAvatar->requestId = id;
|
netAvatar->requestId = id;
|
||||||
OCTET_STRING_fromBuf(&netAvatar->avatar,
|
OCTET_STRING_fromBuf(&netAvatar->avatar,
|
||||||
(const char *)info.avatar.data,
|
(const char *)info.avatar.GetData(),
|
||||||
MD5_DATA_SIZE);
|
MD5_DATA_SIZE);
|
||||||
GetSender().Send(GetContext().GetSessionData(), packet);
|
GetSender().Send(GetContext().GetSessionData(), packet);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -969,7 +969,7 @@ ServerLobbyThread::HandleNetPacketInit(SessionWrapper session, const InitMessage
|
|||||||
&& playerName.substr(0, sizeof(SERVER_GUEST_PLAYER_NAME) - 1) == SERVER_GUEST_PLAYER_NAME)
|
&& playerName.substr(0, sizeof(SERVER_GUEST_PLAYER_NAME) - 1) == SERVER_GUEST_PLAYER_NAME)
|
||||||
{
|
{
|
||||||
string guestId(playerName.substr(sizeof(SERVER_GUEST_PLAYER_NAME)));
|
string guestId(playerName.substr(sizeof(SERVER_GUEST_PLAYER_NAME)));
|
||||||
if (count_if(guestId.begin(), guestId.end(), ::isdigit) == guestId.size())
|
if ((size_t)count_if(guestId.begin(), guestId.end(), ::isdigit) == guestId.size())
|
||||||
{
|
{
|
||||||
validGuest = true;
|
validGuest = true;
|
||||||
noAuth = true;
|
noAuth = true;
|
||||||
@@ -998,7 +998,7 @@ ServerLobbyThread::HandleNetPacketInit(SessionWrapper session, const InitMessage
|
|||||||
const UnauthenticatedLogin_t *noauthLogin = &initMessage.login.choice.unauthenticatedLogin;
|
const UnauthenticatedLogin_t *noauthLogin = &initMessage.login.choice.unauthenticatedLogin;
|
||||||
playerName = STL_STRING_FROM_OCTET_STRING(noauthLogin->nickName);
|
playerName = STL_STRING_FROM_OCTET_STRING(noauthLogin->nickName);
|
||||||
if (noauthLogin->avatar)
|
if (noauthLogin->avatar)
|
||||||
memcpy(avatarMD5.data, noauthLogin->avatar->buf, MD5_DATA_SIZE);
|
memcpy(avatarMD5.GetData(), noauthLogin->avatar->buf, MD5_DATA_SIZE);
|
||||||
noAuth = true;
|
noAuth = true;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -1207,7 +1207,7 @@ ServerLobbyThread::HandleNetPacketRetrievePlayerInfo(SessionWrapper session, con
|
|||||||
data->avatarData->avatarType = static_cast<NetAvatarType_t>(AvatarManager::GetAvatarFileType(tmpPlayer->GetAvatarFile()));
|
data->avatarData->avatarType = static_cast<NetAvatarType_t>(AvatarManager::GetAvatarFileType(tmpPlayer->GetAvatarFile()));
|
||||||
OCTET_STRING_fromBuf(
|
OCTET_STRING_fromBuf(
|
||||||
&data->avatarData->avatar,
|
&data->avatarData->avatar,
|
||||||
(char *)tmpPlayer->GetAvatarMD5().data,
|
(char *)tmpPlayer->GetAvatarMD5().GetData(),
|
||||||
MD5_DATA_SIZE);
|
MD5_DATA_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1226,7 +1226,7 @@ ServerLobbyThread::HandleNetPacketRetrieveAvatar(SessionWrapper session, const A
|
|||||||
|
|
||||||
string tmpFile;
|
string tmpFile;
|
||||||
MD5Buf tmpMD5;
|
MD5Buf tmpMD5;
|
||||||
memcpy(tmpMD5.data, retrieveAvatar.avatar.buf, MD5_DATA_SIZE);
|
memcpy(tmpMD5.GetData(), retrieveAvatar.avatar.buf, MD5_DATA_SIZE);
|
||||||
if (GetAvatarManager().GetAvatarFileName(tmpMD5, tmpFile))
|
if (GetAvatarManager().GetAvatarFileName(tmpMD5, tmpFile))
|
||||||
{
|
{
|
||||||
NetPacketList tmpPackets;
|
NetPacketList tmpPackets;
|
||||||
@@ -1444,7 +1444,7 @@ ServerLobbyThread::RequestPlayerAvatar(SessionWrapper session)
|
|||||||
netAvatarRequest->requestId = session.playerData->GetUniqueId();
|
netAvatarRequest->requestId = session.playerData->GetUniqueId();
|
||||||
OCTET_STRING_fromBuf(
|
OCTET_STRING_fromBuf(
|
||||||
&netAvatarRequest->avatar,
|
&netAvatarRequest->avatar,
|
||||||
(char *)session.playerData->GetAvatarMD5().data,
|
(char *)session.playerData->GetAvatarMD5().GetData(),
|
||||||
MD5_DATA_SIZE);
|
MD5_DATA_SIZE);
|
||||||
GetSender().Send(session.sessionData, packet);
|
GetSender().Send(session.sessionData, packet);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user