Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/Common/LtsLicence/aes_crypt.cpp
T
torstenandCopilot 734a5e5315 Qt6 Migration: BUILD_RTI Guards, Export-Header und Include-Pfade korrigiert
- BUILD_RTI Guards in LtsSession implementiert (6 Dateien)
- LtsSessionDataDds Stub für BUILD_RTI=OFF erstellt
- Export-Header Stubs erstellt (GuezNasat, AlarmManagement, MarkerManagement)
- Include-Pfade korrigiert (resource_path.h, logger.h, Proxies)
- QStringRef → QString migriert (Qt6 Kompatibilität)
- Qt6::Gui Dependencies hinzugefügt (LtsElevation, AdvancedSymbology)
- FilterManagement-Plugins temporär vom Build ausgeschlossen
- MOC-Probleme gefixt (staticMetaObject, override final)
- Build erfolgreich: 1940 Targets mit BUILD_RTI=OFF im Docker-Container

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-08-20 12:26:22 +02:00

186 lines
5.6 KiB
C++

#include "aes_crypt.h"
#include <NGLLogger/NGLLoggerBootstrap.h>
#include <QtCore/QByteArray>
#include <QtCore/QRandomGenerator>
#include <openssl/err.h>
#include <openssl/evp.h>
namespace LtsLicence
{
struct AesCryptPrivate
{
AesCryptPrivate()
: passPhrase(QLatin1String("LTS " LTS_VERSION_MAJOR "." LTS_VERSION_MINOR)),
logger(NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("LtsLicenceLib.AesCrypt"))
{
#if OPENSSL_VERSION_NUMBER < 0x1010000fL
ERR_load_crypto_strings();
#endif
}
~AesCryptPrivate()
{
#if OPENSSL_VERSION_NUMBER < 0x1010000fL
ERR_free_strings();
#endif
}
QString passPhrase;
NGL::INGLLogger& logger;
};
Q_GLOBAL_STATIC(AesCryptPrivate, s_Private)
void setPassPhrase(const QString& passPhrase)
{
s_Private->passPhrase = passPhrase;
}
QString passPhrase()
{
return s_Private->passPhrase;
}
QByteArray decryptData(const QByteArray& dataToDecrypt, const QString& pw, bool* bOk)
{
if (bOk)
*bOk = false;
int ret;
int outSize1 = 0;
int outSize2 = 0;
unsigned char key[32];
const QByteArray baPassword = pw.toUtf8();
const unsigned char* dataPw = reinterpret_cast<const unsigned char*>(baPassword.data());
int i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), nullptr, dataPw, baPassword.size(), 5, key, nullptr);
if (i != 32)
{
ERROR_LOG(s_Private->logger) << "Wrong key size - should be 256 bits";
return QByteArray();
}
const QByteArray baSalt = dataToDecrypt.left(EVP_MAX_IV_LENGTH);
const unsigned char* salt = reinterpret_cast<const unsigned char*>(baSalt.data());
const QByteArray bufEncrypted = dataToDecrypt.mid(EVP_MAX_IV_LENGTH);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
ret = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key, salt);
if (ret == 0)
{
EVP_CIPHER_CTX_free(ctx);
char errbuf[256];
ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
ERROR_LOG(s_Private->logger) << "EVP_DecryptInit_ex failed";
return QByteArray();
}
QByteArray bufDecrypted;
bufDecrypted.resize(bufEncrypted.size() + EVP_CIPHER_CTX_block_size(ctx));
const unsigned char* dataIn = reinterpret_cast<const unsigned char*>(bufEncrypted.data());
unsigned char* dataOut = reinterpret_cast<unsigned char*>(bufDecrypted.data());
ret = EVP_DecryptUpdate(ctx, dataOut, &outSize1, dataIn, bufEncrypted.size());
if (ret == 0)
{
EVP_CIPHER_CTX_free(ctx);
char errbuf[256];
ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
ERROR_LOG(s_Private->logger) << "EVP_DecryptUpdate failed";
return QByteArray();
}
ret = EVP_DecryptFinal_ex(ctx, dataOut + outSize1, &outSize2);
if (ret == 0)
{
EVP_CIPHER_CTX_free(ctx);
char errbuf[256];
ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
ERROR_LOG(s_Private->logger) << "EVP_DecryptFinal_ex failed";
return QByteArray();
}
EVP_CIPHER_CTX_free(ctx);
bufDecrypted.resize(outSize1 + outSize2);
if (bOk)
*bOk = true;
return bufDecrypted;
}
QByteArray encryptData(const QByteArray& dataToEncrypt, const QString& pw, bool* bOk)
{
if (bOk)
*bOk = false;
int ret;
int outSize1 = 0;
int outSize2 = 0;
unsigned char key[32];
QByteArray bufEncrypted;
const QByteArray baPassword = pw.toUtf8();
const unsigned char* dataPw = reinterpret_cast<const unsigned char*>(baPassword.data());
int i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), nullptr, dataPw, baPassword.size(), 5, key, nullptr);
if (i != 32)
{
ERROR_LOG(s_Private->logger) << "Wrong key size - should be 256 bits";
return QByteArray();
}
QByteArray baSalt(EVP_MAX_IV_LENGTH, 0);
for (int i = 0; i < EVP_MAX_IV_LENGTH; ++i)
baSalt[i] = static_cast<char>(QRandomGenerator::global()->bounded(256));
const unsigned char* salt = reinterpret_cast<const unsigned char*>(baSalt.data());
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
ret = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key, salt);
if (ret == 0)
{
EVP_CIPHER_CTX_free(ctx);
char errbuf[256];
ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
ERROR_LOG(s_Private->logger) << "EVP_EncryptInit_ex failed";
return QByteArray();
}
bufEncrypted = QByteArray::fromRawData(reinterpret_cast<const char*>(salt), EVP_MAX_IV_LENGTH);
bufEncrypted.resize(bufEncrypted.size() + dataToEncrypt.size() + EVP_CIPHER_CTX_block_size(ctx));
const unsigned char* dataIn = reinterpret_cast<const unsigned char*>(dataToEncrypt.data());
unsigned char* dataOut = reinterpret_cast<unsigned char*>(bufEncrypted.data());
dataOut += baSalt.size();
ret = EVP_EncryptUpdate(ctx, dataOut, &outSize1, dataIn, dataToEncrypt.size());
if (ret == 0)
{
EVP_CIPHER_CTX_free(ctx);
char errbuf[256];
ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
ERROR_LOG(s_Private->logger) << "EVP_EncryptUpdate failed";
return QByteArray();
}
ret = EVP_EncryptFinal_ex(ctx, dataOut + outSize1, &outSize2);
if (ret == 0)
{
EVP_CIPHER_CTX_free(ctx);
char errbuf[256];
ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
ERROR_LOG(s_Private->logger) << "EVP_EncryptFinal_ex failed";
return QByteArray();
}
EVP_CIPHER_CTX_free(ctx);
bufEncrypted.resize(outSize1 + outSize2 + baSalt.size());
if (bOk)
*bOk = true;
return bufEncrypted;
}
} // namespace LtsLicence