bikral 1
bikral
ShadowFon 1
ShadowFon
D 1
delimuratt
PrimeAC 1
PrimeAC
noisiv 1
noisiv
Manwe Work 1
Manwe Work
Best Studio 1
Best Studio
kralhakan2009 1
kralhakan2009
Vahsi Uzman 1
Vahsi Uzman
Hikaye Ekle
Reklam vermek için turkmmo@gmail.com

Önemli: OpenSSL 1.1.X Güncellemesi

dormammu

Level 4
TM Üye
Üye
Katılım
6 Eyl 2017
Konular
53
Mesajlar
358
Çözüm
1
Online süresi
1mo 8d
Reaksiyon Skoru
169
Altın Konu
1
TM Yaşı
8 Yıl 9 Ay 8 Gün
Başarım Puanı
129
Yaş
32
MmoLira
6,490
DevLira
78
Ticaret - 100%
1   0   0

ROHAN2 WORLD 1-120 TR TİPİ OFFICIAL YOHARA, BALATHOR VE AMON! 80. GÜNÜNDE! +10.000 ONLİNE! HİLE VE BOT %100 ENGELLİ HEMEN TIKLA!

Selam arkadaşlar,

Çoğu kaynakta openssl 1.0.x sürümlerinde. Openssl resmi sitesinde geçmiş sürümlerin ciddi açıklarının olduğu yasıyor.

Gerçi metin2 kaynak dosyalarında openssl sadece serverkeyde kullanılıyor bildiğim kadarı ile o yüsden pek bi sararı da olmas.

Yinede güncel openssl için yapmanıs gerekenleri şöyle yasayım;

1-) Openssl 1.1.x(o an ki hangisi güncel ise) makinanisa indirip derleyin libleri oluşturun.
2-) Çıkış lib isimleri eski lib isimlerinden farklı olabilir kaynak kodunusda eski isim ile yeni lib isimlerini değiştirmeyi unutmayın.
3-) Openssl güncellemesi ile libserverkey de ki rsacrypto.cpp de uyumsusluklar mevcut aşağıdaki ile değiştirin;

[CODE lang="cpp" title="RSACrypto.cpp"]#include "RSACrypto.h"
#ifdef _WIN32
#include <atlenc.h>
#endif
#include <assert.h>
#include <openssl/aes.h>
#include <openssl/rsa.h>
#include <openssl/rand.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/engine.h>
#include <openssl/sha.h>
#include <openssl/pem.h>

namespace Security
{
static const char rnd_seed[] = "alsfkdj#$^#Y$JBGVKA()#$J@J#OTJG)(@JG)@GJ)J@$)JG)$JG)GJ#@)G";
class StaticInitializer
{
public:
StaticInitializer()
{
}
~StaticInitializer()
{
RAND_cleanup();
}

void Init()
{
RAND_seed(rnd_seed, sizeof(rnd_seed));
}
};

void InitRandomSeed()
{
static StaticInitializer s;
s.Init();
}

// RSA cryptography
//RSACrypto::PublicKey::PublicKey(const unsigned char* n, int nsize, const unsigned char* e, int esize)
//{
// rsa_ = Alloc();

// BIGNUM* tmp = NULL;
// tmp = BN_bin2bn( n, nsize, rsa_->n );
// assert( tmp );
// tmp = BN_bin2bn( e, esize, rsa_->e );
// assert( tmp );

// //#ifdef _DEBUG
// // printf("n:");
// // BN_print_fp(stdout, rsa_->n);
// // printf("");
// // printf("e:");
// // BN_print_fp(stdout, rsa_->e);
// // printf("");
// //#endif
//}

RSACrypto::PublicKey::PublicKey(const char* n, const char* e)
{
rsa_ = Alloc();

BN_hex2bn((BIGNUM**)RSA_get0_n(rsa_), n);
BN_hex2bn((BIGNUM**)RSA_get0_e(rsa_), e);
}

RSACrypto::PublicKey::PublicKey(RSACrypto::PublicKey& p)
{
rsa_ = RSA_new();
assert(rsa_);
Copy(rsa_, p.rsa_);
}

RSACrypto::PublicKey::~PublicKey()
{
Free(rsa_);
rsa_ = NULL;
}

RSACrypto::PublicKey& RSACrypto::PublicKey::operator =(const RSACrypto::PublicKey& p)
{
if (rsa_)
{
Free(rsa_);
rsa_ = NULL;
}

rsa_ = RSA_new();
assert(rsa_);
Copy(rsa_, p.rsa_);

return *this;
}

//Buffer RSACrypto::PublicKey::GetN()
//{
// if ( rsa_ )
// {
// int len = BN_num_bytes( rsa_->n );
// Buffer n = Buffer::Alloc( len );
// if ( NULL == n.buf )
// {
// return Buffer();
// }

// BN_bn2bin( rsa_->n, (unsigned char *)n.buf );
// return n;
// }

// return Buffer();
//}

//Buffer RSACrypto::PublicKey::GetE()
//{
// if ( rsa_ )
// {
// int len = BN_num_bytes( rsa_->e );
// Buffer e = Buffer::Alloc( len );
// if ( NULL == e.buf )
// {
// return Buffer();
// }

// BN_bn2bin( rsa_->e, (unsigned char *)e.buf );
// return e;
// }

// return Buffer();
//}

RSACrypto::PublicKey::PublicKey() : rsa_(NULL)
{
}

RSA* RSACrypto::PublicKey::Alloc()
{
RSA* rsa = RSA_new();
assert(rsa);

RSA_set0_key(rsa, BN_new(), BN_new(), BN_new());

return rsa;
}

void RSACrypto::PublicKey::Free(rsa_st* p)
{
if (p)
{
RSA_free(p);
}
}

void RSACrypto::PublicKey::Copy(rsa_st* to, const rsa_st* from)
{
BN_copy((BIGNUM*)RSA_get0_n(to), RSA_get0_n(from));
BN_copy((BIGNUM*)RSA_get0_e(to), RSA_get0_e(from));
}

RSACrypto::PrivateKey::PrivateKey() : rsa_(NULL)
{
}

RSACrypto::PrivateKey::PrivateKey(rsa_st* rsa) : rsa_(rsa)
{
}

RSACrypto::PrivateKey::~PrivateKey()
{
if (rsa_)
{
RSA_free(rsa_);
rsa_ = NULL;
}
}

RSA* RSACrypto::PrivateKey::Alloc()
{
RSA* rsa = RSA_new();
assert(rsa);
RSA_set0_key(rsa, BN_new(), BN_new(), BN_new());
RSA_set0_factors(rsa, BN_new(), BN_new());

return rsa;
}

Buffer RSACrypto::EncryptPublic(const RSACrypto::PublicKey* k, const unsigned char* plain, int plainLen)
{
int rsaSize = RSA_size(k->rsa_);
Buffer cipherText = Buffer::Alloc(rsaSize);
if (NULL == cipherText.buf)
{
return Buffer();
}

// must be checked when RSA_PKCS1_OAEP_PADDING mode
if (plainLen >= rsaSize - 41)
{
Buffer::Free(cipherText);
assert(false);
return Buffer();
}

int cipherTextLen = RSA_public_encrypt(
plainLen,
plain,
(unsigned char*)cipherText.buf,
k->rsa_,
RSA_PKCS1_OAEP_PADDING);
if (-1 == cipherTextLen)
{
Buffer::Free(cipherText);
return Buffer();
}

assert(cipherTextLen == rsaSize);
//XSystem::MemoryPool::MemoryPool_Realloc( cipherText, cipherTextLen );
return cipherText;
}

Buffer RSACrypto::DecryptPrivate(const RSACrypto::PrivateKey* k, const unsigned char* cipher, int cipherLen)
{
int rsaSize = RSA_size(k->rsa_);
Buffer plainText = Buffer::Alloc(rsaSize);
if (NULL == plainText.buf)
{
return Buffer();
}

int plainTextLen = RSA_private_decrypt(
cipherLen,
cipher,
(unsigned char*)plainText.buf,
k->rsa_,
RSA_PKCS1_OAEP_PADDING);
if (-1 == plainTextLen)
{
Buffer::Free(plainText);
return Buffer();
}

plainText.len = plainTextLen;

return plainText;
}

Buffer RSACrypto::EncryptPrivate(const RSACrypto::PrivateKey* k, const unsigned char* plain, int plainLen)
{
int rsaSize = RSA_size(k->rsa_);
Buffer cipherText = Buffer::Alloc(rsaSize);
if (NULL == cipherText.buf)
{
return Buffer();
}

// must be checked when RSA_PKCS1_PADDING mode (private encrypt¿¡¼´Â ´Ù¸¥ paddingÀ» Áö¿ø¾ÈÇÑ´Ù.)
if (plainLen >= rsaSize - 11)
{
Buffer::Free(cipherText);
assert(false);
return Buffer();
}

int cipherTextLen = RSA_private_encrypt(
plainLen,
plain,
(unsigned char*)cipherText.buf,
k->rsa_,
RSA_PKCS1_PADDING);
if (-1 == cipherTextLen)
{
Buffer::Free(cipherText);
return Buffer();
}

assert(cipherTextLen == rsaSize);
//XSystem::MemoryPool::MemoryPool_Realloc( cipherText, cipherTextLen );
return cipherText;
}

Buffer RSACrypto::DecryptPublic(const RSACrypto::PublicKey* k, const unsigned char* cipher, int cipherLen)
{
int rsaSize = RSA_size(k->rsa_);
Buffer plainText = Buffer::Alloc(rsaSize);
if (NULL == plainText.buf)
{
return Buffer();
}

int plainTextLen = RSA_public_decrypt(
cipherLen,
cipher,
(unsigned char*)plainText.buf,
k->rsa_,
RSA_PKCS1_PADDING);
if (-1 == plainTextLen)
{
Buffer::Free(plainText);
return Buffer();
}

plainText.len = plainTextLen;

return plainText;
}

bool RSACrypto::GenerateKey(RSACrypto::PublicKey* publicKey, RSACrypto::PrivateKey* privateKey)
{
RSA* rsa = RSA_generate_key(1024, 7, NULL, NULL);
if (NULL == rsa)
{
//ERR_get_error();
return false;
}

if (1 != RSA_check_key(rsa))
{
//ERR_get_error();
return false;
}

publicKey->rsa_ = publicKey->Alloc();
publicKey->Copy(publicKey->rsa_, rsa);
privateKey->rsa_ = rsa;

#ifdef _DEBUG
// printf("n:");
// BN_print_fp(stdout, publicKey->rsa_->n);
// printf("");
// printf("e:");
// BN_print_fp(stdout, publicKey->rsa_->e);
// printf("");

//char buf[1024];
//BIO* bp = BIO_new_mem_buf(buf, sizeof(buf));
//PEM_write_bio_RSAPrivateKey(bp, rsa, 0, 0, 0, 0, 0);

//PEM_read_bio_RSAPrivateKey(bp, )
#endif

return true;
}

bool RSACrypto::PrintKey(const PublicKey* k, std::string& e, std::string& n)
{
char* tmp = BN_bn2hex(RSA_get0_e(k->rsa_));
if (!tmp)
{
return false;
}
e = tmp;

tmp = BN_bn2hex(RSA_get0_n(k->rsa_));
if (!tmp)
{
return false;
}
n = tmp;

return true;
}

bool RSACrypto::PrintKey(const PrivateKey* k, std::string& n, std::string& e, std::string& d)
{
char* tmp = BN_bn2hex(RSA_get0_n(k->rsa_));
if (!tmp)
{
return false;
}
n = tmp;

tmp = BN_bn2hex(RSA_get0_e(k->rsa_));
if (!tmp)
{
return false;
}
e = tmp;

tmp = BN_bn2hex(RSA_get0_d(k->rsa_));
if (!tmp)
{
return false;
}
d = tmp;

return true;
}

bool RSACrypto::StorePrivateKey(const PrivateKey* k, char* buf, size_t& buflen)
{
// DER Æ÷¸ËÀ¸·Î º¯È¯
char* tmp = NULL;
int n = i2d_RSAPrivateKey(k->rsa_, (unsigned char**)&tmp);
if (n < 0)
{
return false;
}

if (n > (int)buflen)
{
printf("RSACrypto::StorePrivateKey: buflen is too small\n");
return false;
}

memcpy(buf, tmp, n);
buflen = n;

free(tmp);
return true;
}

bool RSACrypto::RestorePrivateKey(const char* buf, size_t buflen, PrivateKey* k)
{
// DER Æ÷¸Ë¿¡¼ º¯È¯
char* tmp = (char*)malloc(buflen);
if (!tmp)
{
return false;
}
memcpy(tmp, buf, buflen);

if (!d2i_RSAPrivateKey(&k->rsa_, (const unsigned char**)&tmp, buflen))
{
free(tmp);
return false;
}

return true;
}

bool RSACrypto::StorePublicKey(const PublicKey* k, char* buf, size_t& buflen)
{
// DER Æ÷¸ËÀ¸·Î º¯È¯
char* tmp = NULL;
int n = i2d_RSAPublicKey(k->rsa_, (unsigned char**)&tmp);
if (n < 0)
{
return false;
}

if (n > (int)buflen)
{
printf("RSACrypto::StorePublicKey: buflen is too small\n");
return false;
}

memcpy(buf, tmp, n);
buflen = n;

free(tmp);
return true;
}

bool RSACrypto::RestorePublicKey(const char* buf, size_t buflen, PublicKey* k)
{
// DER Æ÷¸Ë¿¡¼ º¯È¯
char* tmp = (char*)malloc(buflen);
if (!tmp)
{
return false;
}
memcpy(tmp, buf, buflen);

if (!d2i_RSAPublicKey(&k->rsa_, (const unsigned char**)&tmp, buflen))
{
free(tmp);
return false;
}

return true;
}

Buffer SHA1::Digest(const Buffer& plain)
{
Buffer result = Buffer::Alloc(20);
::SHA1((const unsigned char*)plain.buf,
(unsigned long)plain.len,
(unsigned char*)result.buf);

return result;
}
}
[/CODE]

Bukadar.

@Koray' müsait samanında konuyu yeşillendirebilir misin?
 

En Çok Reaksiyon Alan Mesajlar

Teşekkürler
1593444557211.png
 

Şu an konuyu görüntüleyenler (Toplam : 0, Üye: 0, Misafir: 0)

Geri
Üst