Add flexibility in login packets

* It's now possible to add custom data in the login packet
* Add utility funciton to generate RSA keys
* Make the protocol able to use RSA keys with 2048 bits or more
This commit is contained in:
Eduardo Bart
2013-01-28 20:52:03 -02:00
parent 6c7a163197
commit b7eef97239
17 changed files with 84 additions and 103 deletions

View File

@@ -117,9 +117,11 @@ void Application::registerLuaFunctions()
g_lua.bindSingletonFunction("g_crypt", "decrypt", &Crypt::decrypt, &g_crypt);
g_lua.bindSingletonFunction("g_crypt", "sha1Encode", &Crypt::sha1Encode, &g_crypt);
g_lua.bindSingletonFunction("g_crypt", "md5Encode", &Crypt::md5Encode, &g_crypt);
g_lua.bindSingletonFunction("g_crypt", "rsaGenerateKey", &Crypt::rsaGenerateKey, &g_crypt);
g_lua.bindSingletonFunction("g_crypt", "rsaSetPublicKey", &Crypt::rsaSetPublicKey, &g_crypt);
g_lua.bindSingletonFunction("g_crypt", "rsaSetPrivateKey", &Crypt::rsaSetPrivateKey, &g_crypt);
g_lua.bindSingletonFunction("g_crypt", "rsaCheckKey", &Crypt::rsaCheckKey, &g_crypt);
g_lua.bindSingletonFunction("g_crypt", "rsaGetSize", &Crypt::rsaGetSize, &g_crypt);
// Clock
g_lua.registerSingletonClass("g_clock");

View File

@@ -89,12 +89,14 @@ void OutputMessage::addPaddingBytes(int bytes, uint8 byte)
m_messageSize += bytes;
}
void OutputMessage::encryptRsa(int size)
void OutputMessage::encryptRsa()
{
int size = g_crypt.rsaGetSize();
if(m_messageSize < size)
throw stdext::exception("insufficient bytes in buffer to encrypt");
g_crypt.rsaEncrypt((unsigned char*)m_buffer + m_writePos - size, size);
if(!g_crypt.rsaEncrypt((unsigned char*)m_buffer + m_writePos - size, size))
throw stdext::exception("rsa encryption failed");
}
void OutputMessage::writeChecksum()

View File

@@ -49,7 +49,7 @@ public:
void addString(const std::string& buffer);
void addPaddingBytes(int bytes, uint8 byte = 0);
void encryptRsa(int size);
void encryptRsa();
uint16 getWritePos() { return m_writePos; }
uint16 getMessageSize() { return m_messageSize; }

View File

@@ -292,11 +292,21 @@ std::string Crypt::sha512Encode(const std::string& decoded_string, bool upperCas
return result;
}
void Crypt::rsaGenerateKey(int bits, int e)
{
RSA *rsa = RSA_generate_key(bits, e, nullptr, nullptr);
g_logger.info(stdext::format("%d bits (%d bytes) RSA key generated", bits, bits / 8));
g_logger.info(std::string("p = ") + BN_bn2dec(m_rsa->p));
g_logger.info(std::string("q = ") + BN_bn2dec(m_rsa->q));
g_logger.info(std::string("d = ") + BN_bn2dec(m_rsa->d));
g_logger.info(std::string("n = ") + BN_bn2dec(m_rsa->n));
g_logger.info(std::string("e = ") + BN_bn2dec(m_rsa->e));
RSA_free(rsa);
}
void Crypt::rsaSetPublicKey(const std::string& n, const std::string& e)
{
RSA_free(m_rsa);
m_rsa = RSA_new();
BN_dec2bn(&m_rsa->n, n.c_str());
BN_dec2bn(&m_rsa->e, e.c_str());
}
@@ -331,12 +341,20 @@ bool Crypt::rsaCheckKey()
bool Crypt::rsaEncrypt(unsigned char *msg, int size)
{
assert(size <= 128);
if(size != RSA_size(m_rsa))
return false;
return RSA_public_encrypt(size, msg, msg, m_rsa, RSA_NO_PADDING) != -1;
}
bool Crypt::rsaDecrypt(unsigned char *msg, int size)
{
assert(size <= 128);
if(size != RSA_size(m_rsa))
return false;
return RSA_private_decrypt(size, msg, msg, m_rsa, RSA_NO_PADDING) != -1;
}
int Crypt::rsaGetSize()
{
return RSA_size(m_rsa);
}

View File

@@ -49,11 +49,13 @@ public:
std::string sha256Encode(const std::string& decoded_string, bool upperCase);
std::string sha512Encode(const std::string& decoded_string, bool upperCase);
void rsaGenerateKey(int bits, int e);
void rsaSetPublicKey(const std::string& n, const std::string& e);
void rsaSetPrivateKey(const std::string &p, const std::string &q, const std::string &d);
bool rsaCheckKey();
bool rsaEncrypt(unsigned char *msg, int size);
bool rsaDecrypt(unsigned char *msg, int size);
int rsaGetSize();
private:
std::string getMachineKey();