Move RSA class to Crypt

This commit is contained in:
Henrique Santiago
2012-08-04 10:54:35 -03:00
parent fa15c25951
commit fb7ab21e71
15 changed files with 120 additions and 159 deletions

View File

@@ -22,16 +22,31 @@
#include "crypt.h"
#include <framework/stdext/math.h>
#include <framework/core/logger.h>
#include <boost/uuid/uuid.hpp>
#include <boost/functional/hash.hpp>
#include <openssl/sha.h>
#include <openssl/md5.h>
#include <openssl/bn.h>
#include <openssl/err.h>
static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static inline bool is_base64(unsigned char c) { return (isalnum(c) || (c == '+') || (c == '/')); }
Crypt g_crypt;
Crypt::Crypt()
{
m_rsa = RSA_new();
}
Crypt::~Crypt()
{
RSA_free(m_rsa);
}
std::string Crypt::base64Encode(const std::string& decoded_string)
{
std::string ret;
@@ -204,3 +219,49 @@ std::string Crypt::sha1Encode(std::string decoded_string, bool upperCase)
std::transform(result.begin(), result.end(), result.begin(), tolower);
return result;
}
void Crypt::rsaSetPublicKey(const std::string& n, const std::string& e)
{
BN_dec2bn(&m_rsa->n, n.c_str());
BN_dec2bn(&m_rsa->e, e.c_str());
}
void Crypt::rsaSetPrivateKey(const std::string& p, const std::string& q, const std::string& d)
{
BN_dec2bn(&m_rsa->p, p.c_str());
BN_dec2bn(&m_rsa->q, q.c_str());
BN_dec2bn(&m_rsa->d, d.c_str());
}
bool Crypt::rsaCheckKey()
{
// only used by server, that sets both public and private
if(RSA_check_key(m_rsa)) {
BN_CTX *ctx = BN_CTX_new();
BN_CTX_start(ctx);
BIGNUM *r1 = BN_CTX_get(ctx), *r2 = BN_CTX_get(ctx);
BN_mod(m_rsa->dmp1, m_rsa->d, r1, ctx);
BN_mod(m_rsa->dmq1, m_rsa->d, r2, ctx);
BN_mod_inverse(m_rsa->iqmp, m_rsa->q, m_rsa->p, ctx);
return true;
}
else {
ERR_load_crypto_strings();
g_logger.error(stdext::format("RSA check failed - %s", ERR_error_string(ERR_get_error(), NULL)));
return false;
}
}
bool Crypt::rsaEncrypt(unsigned char *msg, int size)
{
assert(size <= 128);
return RSA_public_encrypt(size, msg, msg, m_rsa, RSA_NO_PADDING) != -1;
}
bool Crypt::rsaDecrypt(unsigned char *msg, int size)
{
assert(size <= 128);
return RSA_private_decrypt(size, msg, msg, m_rsa, RSA_NO_PADDING) != -1;
}

View File

@@ -26,7 +26,14 @@
#include "../stdext/types.h"
#include <string>
namespace Crypt {
#include <openssl/rsa.h>
class Crypt
{
public:
Crypt();
~Crypt();
std::string base64Encode(const std::string& decoded_string);
std::string base64Decode(const std::string& encoded_string);
std::string xorCrypt(const std::string& buffer, const std::string& key);
@@ -35,6 +42,17 @@ namespace Crypt {
std::string decrypt(const std::string& encrypted_string);
std::string md5Encode(std::string decoded_string, bool upperCase);
std::string sha1Encode(std::string decoded_string, bool upperCase);
}
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);
private:
RSA *m_rsa;
};
extern Crypt g_crypt;
#endif

View File

@@ -1,82 +0,0 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "rsa.h"
#include <openssl/bn.h>
#include <openssl/err.h>
Rsa g_rsa;
Rsa::Rsa()
{
m_rsa = RSA_new();
}
Rsa::~Rsa()
{
RSA_free(m_rsa);
}
void Rsa::setPublic(const char *n, const char *e)
{
BN_dec2bn(&m_rsa->n, n);
BN_dec2bn(&m_rsa->e, e);
}
void Rsa::setPrivate(const char *p, const char *q, const char *d)
{
BN_dec2bn(&m_rsa->p, p);
BN_dec2bn(&m_rsa->q, q);
BN_dec2bn(&m_rsa->d, d);
}
bool Rsa::check() // only used by server, that sets both public and private
{
if(RSA_check_key(m_rsa)) {
BN_CTX *ctx = BN_CTX_new();
BN_CTX_start(ctx);
BIGNUM *r1 = BN_CTX_get(ctx), *r2 = BN_CTX_get(ctx);
BN_mod(m_rsa->dmp1, m_rsa->d, r1, ctx);
BN_mod(m_rsa->dmq1, m_rsa->d, r2, ctx);
BN_mod_inverse(m_rsa->iqmp, m_rsa->q, m_rsa->p, ctx);
return true;
}
else {
ERR_load_crypto_strings();
g_logger.error(stdext::format("RSA check failed - %s", ERR_error_string(ERR_get_error(), NULL)));
return false;
}
}
bool Rsa::encrypt(unsigned char *msg, int size)
{
assert(size <= 128);
return RSA_public_encrypt(size, msg, msg, m_rsa, RSA_NO_PADDING) != -1;
}
bool Rsa::decrypt(unsigned char *msg, int size)
{
assert(size <= 128);
return RSA_private_decrypt(size, msg, msg, m_rsa, RSA_NO_PADDING) != -1;
}

View File

@@ -1,48 +0,0 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef RSA_H
#define RSA_H
#include <framework/global.h>
#include <openssl/rsa.h>
class Rsa
{
public:
Rsa();
~Rsa();
void setPublic(const char *n, const char *e);
void setPrivate(const char *p, const char *q, const char *d);
bool check();
bool encrypt(unsigned char *msg, int size);
bool decrypt(unsigned char *msg, int size);
private:
RSA *m_rsa;
};
extern Rsa g_rsa;
#endif