Continues migration with DDS, Common

This commit is contained in:
torsten committed 2026-07-17 12:41:42 +02:00
1 parent 9b15cee841
commit fa48db111e
668 files changed
+111841 -2

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
#
# $Id$
# $HeadURL$
# $Author$
# Created by hjl, SLSE, 2016-05-31
#
FIND_PACKAGE(OpenSSL REQUIRED)
SET(ltslicence_SRCS
aes_crypt.cpp
aes_crypt.h
lts_licence.cpp
lts_licence.h
)
NGL_ADD_LIBRARY2(AAR SV WS LtsLicence SHARED ${ltslicence_SRCS})
NGL_TARGET_INCLUDE_DIRECTORIES(LtsLicence
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../
)
NGL_TARGET_LINK_LIBRARIES(LtsLicence
PRIVATE
${OPENSSL_LIBRARIES}
Qt5::Xml
NGLLogger
PUBLIC
Qt5::Core
)
IF (BUILD_TESTING AND TARGET LtsLicence)
ADD_EXECUTABLE(aes_crypt_test aes_crypt_test.cpp aes_crypt_test.h)
TARGET_LINK_LIBRARIES(aes_crypt_test
LtsLicence
NGLLogger
Qt5::Core
Qt5::Test
)
ADD_TEST(NAME aes_crypt_test COMMAND $<TARGET_FILE:aes_crypt_test>)
ENDIF()
+232
View File
@@ -0,0 +1,232 @@
#include "aes_crypt.h"
#include <QtCore/QByteArray>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <NGLLogger/NGLLoggerBootstrap.h>
namespace LtsLicence
{
struct AesCryptPrivate
{
AesCryptPrivate()
: passPhrase(QLatin1String("LTS " LTS_VERSION_MAJOR "." LTS_VERSION_MINOR))
, logger(NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("LtsLicenceLib.AesCrypt"))
{
qsrand(time(nullptr));
#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] = qrand();
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;
}
}
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include <QtCore/QString>
#include <LtsLicence/ltslicence_export.h>
namespace LtsLicence
{
LTSLICENCE_EXPORT void setPassPhrase(const QString &passPhrase);
LTSLICENCE_EXPORT QString passPhrase();
LTSLICENCE_EXPORT QByteArray decryptData(const QByteArray &dataToDecrypt,
const QString &pw = passPhrase(),
bool *bOk = nullptr);
LTSLICENCE_EXPORT QByteArray encryptData(const QByteArray &dataToEncrypt,
const QString &pw = passPhrase(),
bool *bOk = nullptr);
}
+54
View File
@@ -0,0 +1,54 @@
#include <QtCore/QCoreApplication>
#include <QtTest/QTest>
#include <NGLLogger/NGLLoggerBootstrap.h>
#include <openssl/evp.h>
#include "aes_crypt.h"
#include "aes_crypt_test.h"
QTEST_MAIN(aes_crypt_test)
void aes_crypt_test::initTestCase()
{
NGL::NGLLoggerBootstrap::createNGLRootLogger("log4cxx.props", false);
}
void aes_crypt_test::truePassTest()
{
qDebug("Simple TestCase");
bool bOk;
const QByteArray content("30.06.2013");
const QString pw(QStringLiteral("aes_crypt_test"));
const QByteArray enc = LtsLicence::encryptData(content, pw, &bOk);
QVERIFY2(bOk, "Error encrypting data...");
QCOMPARE(((content.size() + 15 + EVP_MAX_IV_LENGTH) / 18) * 16, enc.size());
const QByteArray dec = LtsLicence::decryptData(enc, pw, &bOk);
QVERIFY2(bOk, "Error encrypting data...");
QCOMPARE(dec, content);
const QByteArray bigContent(12345, '1');
const QByteArray bigEnc = LtsLicence::encryptData(bigContent, pw, &bOk);
QVERIFY2(bOk, "Error encrypting data...");
QCOMPARE(((bigContent.size() + 15 + EVP_MAX_IV_LENGTH) / 16) * 16, bigEnc.size());
}
void aes_crypt_test::falsePassTest()
{
qDebug("False Password TestCase");
bool bOk;
const QByteArray content("30.06.2013");
const QString pw(QStringLiteral("aes_crypt_test"));
const QString pw2(QStringLiteral("aes_crypt_test2"));
const QByteArray enc = LtsLicence::encryptData(content, pw, &bOk);
QVERIFY2(bOk, "Error encrypting data...");
QVERIFY2(enc.size() != 20, "Error encrypting data...");
const QByteArray dec = LtsLicence::decryptData(enc, pw2, &bOk);
QVERIFY2(!bOk, "Error encrypting data...");
QVERIFY2(dec.isEmpty(), "decrypted data is not empty...");
}
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include <QtCore/QObject>
class aes_crypt_test : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void truePassTest();
void falsePassTest();
};
+173
View File
@@ -0,0 +1,173 @@
#include "lts_licence.h"
#include <QDomDocument>
#include <NGLLogger/NGLLoggerBootstrap.h>
#include <QObject>
namespace LtsLicence
{
static bool extractLicence(const QDomElement &licenceElement, QVector<Licence> &licenceVec);
static NGL::INGLLogger &getLogger()
{
return NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("LtsLicence");
}
QVector<Licence> readLicences(const QByteArray &unencryptedXmlData)
{
QVector<Licence> licenceVec;
QDomDocument xmlDoc;
QString errorMsg;
int errorLine = 0;
int errorColumn = 0;
if (!xmlDoc.setContent(unencryptedXmlData, &errorMsg, &errorLine, &errorColumn))
{
ERROR_LOG(getLogger()) << "Error reading xml data, " << "error in line " << errorLine
<< ", column " << errorColumn
<< ", message: " << errorMsg;
return licenceVec;
}
INFO_LOG(getLogger()) << "Try to extract licences";
QDomElement licenceElement = xmlDoc.documentElement().firstChildElement(QLatin1String("licence"));
while (!licenceElement.isNull())
{
if (!extractLicence(licenceElement, licenceVec))
{
licenceVec.clear();
return licenceVec;
}
licenceElement = licenceElement.nextSiblingElement(QLatin1String("licence"));
}
return licenceVec;
}
QByteArray writeUnencryptedLicences(const QVector<Licence> &licences)
{
QDomDocument xmlDoc;
QDomProcessingInstruction instr = xmlDoc.createProcessingInstruction(QLatin1String("xml"),
QLatin1String("version='1.0' encoding='UTF-8' standalone='yes'"));
xmlDoc.appendChild(instr);
QDomElement root = xmlDoc.createElement(QLatin1String("licences"));
xmlDoc.appendChild(root);
for (int i = 0; i < licences.size(); ++i)
{
QDomElement lic = xmlDoc.createElement(QLatin1String("licence"));
root.appendChild(lic);
QDomElement element = xmlDoc.createElement(QLatin1String("name"));
QDomText text = xmlDoc.createTextNode(licences[i].name);
element.appendChild(text);
lic.appendChild(element);
element = xmlDoc.createElement(QLatin1String("ignoreMaxCount"));
text = xmlDoc.createTextNode(QString::number(licences[i].ignoreMaxCount));
element.appendChild(text);
lic.appendChild(element);
element = xmlDoc.createElement(QLatin1String("ignoreExpiration"));
text = xmlDoc.createTextNode(QString::number(licences[i].ignoreExpiration));
element.appendChild(text);
lic.appendChild(element);
element = xmlDoc.createElement(QLatin1String("maxCount"));
text = xmlDoc.createTextNode(QString::number(licences[i].maxCount));
element.appendChild(text);
lic.appendChild(element);
const QLatin1String format("dd.MM.yyyy hh:mm:ss");
const QString qs = licences[i].expiration.toString(format);
element = xmlDoc.createElement(QLatin1String("expiration"));
text = xmlDoc.createTextNode(qs);
element.appendChild(text);
lic.appendChild(element);
}
INFO_LOG(getLogger()) << "Generated XML Data...\n" << xmlDoc.toByteArray();
return xmlDoc.toByteArray();
}
bool extractLicence(const QDomElement &licenceElement, QVector<Licence> &licenceVec)
{
Licence licence;
QDomElement tag = licenceElement.firstChildElement(QLatin1String("name"));
licence.name = tag.text();
tag = licenceElement.firstChildElement(QLatin1String("ignoreMaxCount"));
licence.ignoreMaxCount = tag.text().toInt();
tag = licenceElement.firstChildElement(QLatin1String("ignoreExpiration"));
licence.ignoreExpiration = tag.text().toInt();
tag = licenceElement.firstChildElement(QLatin1String("maxCount"));
licence.maxCount = tag.text().toInt();
tag = licenceElement.firstChildElement(QLatin1String("expiration"));
const QString qs(tag.text());
const QLatin1String format("dd.MM.yyyy hh:mm:ss");
const QDateTime qdt = QDateTime::fromString(qs, format);
if (!qdt.isValid())
{
ERROR_LOG(getLogger()) << "Invalid expiration format! " << qs
<< " used format: " << format
;
return false;
}
licence.expiration = qdt;
INFO_LOG(getLogger()) << "Read licence for:'" << licence.name << "'"
<< " IgnoreMaxCount: " << licence.ignoreMaxCount
<< " IgnoreExpiration: " << licence.ignoreExpiration
<< " MaxCount: " << licence.maxCount
<< " Expiration: " << licence.expiration.date().toString()
<< " " << licence.expiration.time().toString()
<< " RefCount: " << licence.licenceRefCount
;
licenceVec.append(licence);
return true;
}
QString Licence::toString() const
{
return QObject::tr("Name: %1, Count: %2, ExpireDate: %3, Expires: %4, UseMaxCount: %5")
.arg(name)
.arg(maxCount)
.arg(expiration.toString())
.arg(ignoreExpiration)
.arg(ignoreMaxCount);
}
}
+33
View File
@@ -0,0 +1,33 @@
#pragma once
#include <LtsLicence/ltslicence_export.h>
#include <QString>
#include <QVector>
#include <QDateTime>
namespace LtsLicence
{
struct LTSLICENCE_EXPORT Licence
{
QString name;
bool ignoreMaxCount = false;
bool ignoreExpiration = false;
int maxCount = 0;
QDateTime expiration;
int licenceRefCount = 0;
QString toString() const;
};
LTSLICENCE_EXPORT QVector<Licence> readLicences(const QByteArray &unencryptedXmlData);
LTSLICENCE_EXPORT QByteArray writeUnencryptedLicences(const QVector<Licence> &licences);
}