4
1
mirror of https://github.com/QuasarApp/Heart.git synced 2025-05-10 00:19:41 +00:00

Merge branch 'task_313_CheatCard' of github.com:QuasarApp/Heart into task_313_CheatCard

This commit is contained in:
Andrei Yankovich 2022-07-23 22:41:57 +03:00
commit 93f2a096b2
17 changed files with 303 additions and 110 deletions

@ -316,6 +316,29 @@ bool AbstractNode::generateRSAforSSL(EVP_PKEY *pkey) const {
return false;
}
//#if OPENSSL_VERSION_MAJOR >= 3
// EVP_PKEY_CTX *pctx =
// EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
// unsigned int primes = 3;
// unsigned int bits = 4096;
// OSSL_PARAM params[3];
// pkey = EVP_RSA_gen(4096);
// EVP_PKEY_keygen_init(pctx);
// params[0] = OSSL_PARAM_construct_uint("bits", &bits);
// params[1] = OSSL_PARAM_construct_uint("primes", &primes);
// params[2] = OSSL_PARAM_construct_end();
// EVP_PKEY_CTX_set_params(pctx, params);
// EVP_PKEY_generate(pctx, &pkey);
// EVP_PKEY_CTX_free(pctx);
//#else
BIGNUM * bn = BN_new();
int rc = BN_set_word(bn, RSA_F4);
@ -334,7 +357,7 @@ bool AbstractNode::generateRSAforSSL(EVP_PKEY *pkey) const {
q_check_ptr(rsa);
if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0)
return false;
//#endif
return true;
}

@ -672,7 +672,7 @@ protected slots:
* @param errorString This is string value of the error.
* @note default implementation do nothing. Override this method if you want to handle nodes network errors.
*/
virtual void nodeErrorOccured(AbstractNodeInfo *nodeInfo,
virtual void nodeErrorOccured(QH::AbstractNodeInfo *nodeInfo,
QAbstractSocket::SocketError errorCode,
QString errorString);
@ -684,12 +684,12 @@ protected slots:
* Overrid this method for handle ssl errors on this node or server.
* @param error This is error that occured..
*/
virtual void handleSslErrorOcurred(SslSocket *scket, const QSslError& error);
virtual void handleSslErrorOcurred(QH::SslSocket *scket, const QSslError& error);
#endif
private slots:
void avelableBytes(AbstractNodeInfo* sender);
void avelableBytes(QH::AbstractNodeInfo* sender);
/**
* @brief handleNodeStatusChanged This method invoked when status of peer node chganged.
@ -697,7 +697,7 @@ private slots:
* @param status This is new status of node.
*
*/
void handleNodeStatusChanged(AbstractNodeInfo* node, NodeCoonectionStatus status);
void handleNodeStatusChanged(QH::AbstractNodeInfo* node, QH::NodeCoonectionStatus status);
/**
* @brief handleWorkerStoped
@ -708,7 +708,7 @@ private slots:
* @brief handleForceRemoveNode - force remove connection.
* @param node
*/
void handleForceRemoveNode(HostAddress node);
void handleForceRemoveNode(QH::HostAddress node);
/**
* @brief handleBeginWork This method run task on new thread.
@ -723,12 +723,12 @@ private slots:
* Default implementation just pront error messages
* @param errors This is errors list.
*/
void handleSslErrorOcurredPrivate(SslSocket *sender, const QList<QSslError> & errors);
void handleSslErrorOcurredPrivate(QH::SslSocket *sender, const QList<QSslError> & errors);
/**
* @brief handleEncrypted invoke when a ssl socket is encripted!
*/
void handleEncrypted(AbstractNodeInfo *node);
void handleEncrypted(QH::AbstractNodeInfo *node);
#endif

@ -14,7 +14,7 @@
#include "abstractdata.h"
#include "hcrypto/ecdsa.h"
#include "hcrypto/ecdsassl11.h"
#include <asynckeysauth.h>
@ -24,7 +24,7 @@ namespace QH {
/**
* @brief The AuthECDSA class is ecdsa implementation of the Async authentication. This implementation based on Openssl library.
*/
typedef AsyncKeysAuth<ECDSA> AuthECDSA;
typedef AsyncKeysAuth<ECDSASSL11> AuthECDSA;
}
#endif

@ -6,7 +6,7 @@
//#
#include "ecdsa.h"
#include "ecdsassl11.h"
#ifdef USE_HEART_SSL
@ -22,7 +22,7 @@
namespace QH {
ECDSA::ECDSA() {
ECDSASSL11::ECDSASSL11() {
}
@ -79,7 +79,7 @@ QByteArray extractPublicKey(EC_KEY* key, EC_GROUP* group) {
return data;
}
bool ECDSA::makeKeys(QByteArray &pubKey, QByteArray &privKey) {
bool ECDSASSL11::makeKeys(QByteArray &pubKey, QByteArray &privKey) {
EC_KEY *eckey= nullptr;
EC_GROUP *ecgroup = nullptr;
@ -101,7 +101,7 @@ bool ECDSA::makeKeys(QByteArray &pubKey, QByteArray &privKey) {
return pubKey.length() && privKey.length();
}
QByteArray ECDSA::signMessage(const QByteArray &inputData,
QByteArray ECDSASSL11::signMessage(const QByteArray &inputData,
const QByteArray &key) const {
EC_KEY *eckey= nullptr;
@ -147,7 +147,7 @@ QByteArray ECDSA::signMessage(const QByteArray &inputData,
return result;
}
bool ECDSA::checkSign(const QByteArray &inputData,
bool ECDSASSL11::checkSign(const QByteArray &inputData,
const QByteArray &signature,
const QByteArray &key) const {
@ -198,17 +198,17 @@ bool ECDSA::checkSign(const QByteArray &inputData,
}
QByteArray ECDSA::decript(const QByteArray &message, const QByteArray &key) {
QByteArray ECDSASSL11::decript(const QByteArray &message, const QByteArray &key) {
QuasarAppUtils::Params::log("");
return {};
}
QByteArray ECDSA::encript(const QByteArray &message, const QByteArray &key) {
QByteArray ECDSASSL11::encript(const QByteArray &message, const QByteArray &key) {
return {};
}
bool ECDSA::prepareKeyAdnGroupObjects(EC_KEY **eckey, EC_GROUP **ecgroup) {
bool ECDSASSL11::prepareKeyAdnGroupObjects(EC_KEY **eckey, EC_GROUP **ecgroup) {
// input data should be valid pointers to pointers of key and group objects.
if (!(eckey && ecgroup))

@ -6,8 +6,8 @@
//#
#ifndef QH_ECDSA_H
#define QH_ECDSA_H
#ifndef QH_ECDSA_SSL_1_1_H
#define QH_ECDSA_SSL_1_1_H
#include "heart_global.h"
#ifdef USE_HEART_SSL
@ -21,13 +21,14 @@
namespace QH {
/**
* @brief The ECDSA class is ecdsa implementation of the Async authentication. This implementation based on Openssl library.
* @brief The ECDSASSL11 class is ecdsa implementation of the Async authentication. This implementation based on Openssl library.
* @note This class compatibility only with ssl 1.1 and ssl 3.0 (depricated fundtions).
*/
class HEARTSHARED_EXPORT ECDSA: public QH::ICrypto
class HEARTSHARED_EXPORT ECDSASSL11: public QH::ICrypto
{
public:
ECDSA();
ECDSASSL11();
/**
* @brief makeKeys This static method generate the public and private keys of the ECDSA.
@ -62,4 +63,4 @@ private:
#endif
#endif // QH_ECDSA_H
#endif // QH_ECDSA_SSL_1_1_H

@ -61,6 +61,21 @@ QString HostAddress::toString() const {
return QHostAddress::toString() + ":" + QString::number(port());
}
QByteArray HostAddress::toBytes() const {
QByteArray res;
QDataStream stream(&res, QIODevice::WriteOnly);
operator <<(stream, *this);
return res;
}
bool HostAddress::fromBytes(const QByteArray &array) {
if (array.isEmpty())
return false;
QDataStream stream(array);
operator >>(stream, *this);
return true;
}
QDataStream &operator >>(QDataStream &stream, HostAddress &address) {
stream >> static_cast<QHostAddress&>(address);

@ -46,7 +46,7 @@ bool AbstractData::toPackage(Package &package,
bool AbstractData::checkCmd() const {
unsigned int code = typeid (*this).hash_code();
return code == localCode(); \
return code == localCode();
}
bool AbstractData::isValid() const {

@ -124,7 +124,7 @@ class HEARTSHARED_EXPORT AbstractData : public StreamBase
{
public:
virtual ~AbstractData() override;
~AbstractData() override;
/**
* @brief cmd - This is command of this object, (for generate cmd use macross QH_PACKAGE)

@ -53,8 +53,23 @@ public:
_packData.push_back(data);
};
/**
* @brief isValid This implementation check all items of the pack to valid and packa size. The pack size should be more then 0.
* @return true if the pack of items is valid else flase..
*/
bool isValid() const override {
return AbstractData::isValid() && _packData.size();
if (!_packData.size()) {
return false;
}
for (const auto& it: _packData) {
if (!it->isValid()) {
return false;
}
}
return AbstractData::isValid();
};
/**

@ -35,6 +35,18 @@ QByteArray StreamBase::toBytes() const {
return res;
}
bool StreamBase::fromBase64(const QString &data) {
return fromBase64(data.toLatin1());
}
bool StreamBase::fromBase64(const QByteArray &data) {
return fromBytes(QByteArray::fromBase64(data, QByteArray::Base64UrlEncoding));
}
QByteArray StreamBase::toBase64() const {
return toBytes().toBase64(QByteArray::Base64UrlEncoding);
}
unsigned int StreamBase::typeId() const {
return typeid (*this).hash_code();
}

@ -42,6 +42,26 @@ public:
*/
QByteArray toBytes() const;
/**
* @brief fromBase64 This method provide initialization of object from the base64 string.
* @param data This is input base64 data.
* @note converting from the QString is slowly instand of QByteArray, so use the StreamBase::fromBase64(const QByteArray &data) method.
* @return true if all good else false.
*/
bool fromBase64(const QString &data);
/**
* @brief fromBase64 This method provide initialization of object from the base64 string.
* @param data This is input base64 data.
* @return true if all good else false.
*/
bool fromBase64(const QByteArray &data);
/**
* @brief toBase64 This method convert a current object to the base64 string.
* @return base64 string of this object.
*/
QByteArray toBase64() const;
/**
* @brief This is wrapper over toStream.

@ -25,6 +25,7 @@
#include <sqldb.h>
#include "getsinglevalue.h"
#include "setsinglevalue.h"
#include <qaglobalutils.h>
namespace QH {
using namespace PKG;
@ -163,8 +164,22 @@ void DataBase::objectChanged(const QSharedPointer<DBObject> &) {
}
DBPatchMap DataBase::dbPatches() const {
return {};
const DBPatchMap DataBase::dbPatches() const {
return _dbPatches;
}
void DataBase::addDBPatch(const DBPatch &patch) {
debug_assert(patch.isValid(),
"Failed to initialise a Data base patch!"
" Patch object is invalid");
debug_assert(!_dbPatches[patch.versionFrom].contains(patch.versionTo),
"Failed to initialise a Data base patch!");
_dbPatches[patch.versionFrom][patch.versionTo] = patch;
_targetDBVersion = std::max(_targetDBVersion, patch.versionTo);
}
void DataBase::memberSubsribed(const QVariant &, unsigned int ) {
@ -210,13 +225,16 @@ bool DataBase::isForbidenTable(const QString &table) {
}
bool DataBase::upgradeDataBase() {
auto patches = dbPatches();
int actyalyVersion = patches.size();
int currentVersion = 0;
if (!db())
return false;
DBPatchMap patchesPack = dbPatches();
if (!patchesPack.size()) {
return true;
}
int currentVersion = 0;
bool fsupportUpgrade = db()->doQuery("SELECT COUNT(*) FROM DataBaseAttributes", true);
if (!fsupportUpgrade) {
@ -232,31 +250,36 @@ bool DataBase::upgradeDataBase() {
if (auto responce = _db->getObject(request)) {
currentVersion = responce->value().toInt();
}
}
bool fUpdated = false;
while (currentVersion < actyalyVersion) {
auto patch = patches.value(currentVersion, {});
while (currentVersion < _targetDBVersion) {
QString message;
message = "Upgrade data base!. to %0 versions";
message = "Upgrade data base from %0 to %1 versions. %2";
message = message.arg(currentVersion);
QuasarAppUtils::Params::log(message,
QuasarAppUtils::Info);
auto patches = patchesPack.value(currentVersion, {});
if (patch && !patch(db())) {
QuasarAppUtils::Params::log("Failed to " + message,
if (!patches.size()) {
QuasarAppUtils::Params::log("Failed to " + message.arg("Unknown", "Required patch not found!"),
QuasarAppUtils::Error);
return false;
}
currentVersion++;
fUpdated = true;
}
auto patch = patches.last();
message = message.arg(patch.versionTo);
QuasarAppUtils::Params::log(message.arg("(Begin)"),
QuasarAppUtils::Info);
if (!patch.action(db())) {
QuasarAppUtils::Params::log("Failed to " + message.arg("Patch finished with error code!"),
QuasarAppUtils::Error);
return false;
}
currentVersion = patch.versionTo;
if (fUpdated) {
auto updateVersionRequest = QSharedPointer<PKG::SetSingleValue>::create(
DbAddress{"DataBaseAttributes", "version"},
"value", currentVersion, "name");

@ -9,6 +9,7 @@
#define QH_DATABASE_H
#include "abstractnode.h"
#include "dbpatch.h"
#include <dbobject.h>
#include <hostaddress.h>
#include <permission.h>
@ -28,20 +29,6 @@ class DbAddress;
class NodeId;
class iObjectProvider;
/**
* @brief DBPatch This is function that should be upgrade database.
* @see DBPatchMap
* @see DataBaseNode::dbPatch
*/
typedef std::function<bool (const QH::iObjectProvider *)> DBPatch;
/**
* @brief DBPatchMap This is list when index of list is version of database and value if function that should be upgrade database.
* @see DataBaseNode::dbPatch
* @see DBPatchMap
*/
typedef QList<DBPatch> DBPatchMap;
/**
* @brief The DataBase class is DataBase base implementation.
* This implementation contains methods for work with database.
@ -402,31 +389,43 @@ protected:
* Where the 0 version is first version of database. (genesis)
*
* @code{cpp}
* QH::DBPatchMap dbPatches() const {
QH::DBPatchMap result;
addDBPatch({
0, // version
[](const QH::iObjectProvider* database) -> bool {
// Some code for update from 0 to 1
} // action of patch
});
result += [](const QH::iObjectProvider* database) -> bool {
// Some code for update from 0 to 1
};
addDBPatch({
1, // version
[](const QH::iObjectProvider* database) -> bool {
// Some code for update from 1 to 2
} // action of patch
});
result += [](const QH::iObjectProvider* database) -> bool {
// Some code for update from 1 to 2
};
result += [](const QH::iObjectProvider* database) -> bool {
// Some code for update from 2 to 3
};
return result;
}
addDBPatch({
2, // version
[](const QH::iObjectProvider* database) -> bool {
// Some code for update from 2 to 3
} // action of patch
});
* @endcode
*
* @return Map of database pactches.
*
* @see DBPatchMap
* @see DBPatch
* @see DataBase::addDBPatch
*/
virtual DBPatchMap dbPatches() const;
virtual const DBPatchMap dbPatches() const;
/**
* @brief addDBPatch This method add database patch to the data base object.
* @param patch This is object of the database patch
* @note This method will be crashed if patch is invalid.
* @see DataBase::dbPatches
*/
void addDBPatch(const DBPatch& patch);
/**
* @brief upgradeDataBase This method upgrade data base to actyaly database version.
@ -450,6 +449,8 @@ private:
bool isForbidenTable(const QString& table);
ISqlDBCache *_db = nullptr;
unsigned short _targetDBVersion = 0;
DBPatchMap _dbPatches;
QString _localNodeName;
friend class DataBaseNode;

@ -0,0 +1,16 @@
/*
* Copyright (C) 2018-2022 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "dbpatch.h"
namespace QH {
bool DBPatch::isValid() const {
return versionFrom < versionTo && action;
}
}

@ -0,0 +1,51 @@
/*
* Copyright (C) 2018-2022 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#ifndef DBPATCH_H
#define DBPATCH_H
#include "iobjectprovider.h"
namespace QH {
/**
* @brief DBPatch This is function that should be upgrade database.
* @see DBPatchMap
* @see DataBaseNode::dbPatch
*
* @note version is version number of the current database.
* After execute this path version of data base will be increment.
*/
struct DBPatch {
/// This is version of data base that need to up.
/// For example - we has a data base with version 1 and we want to upgrade to version 2 then you need to set cersion field version to 1.
unsigned short versionFrom = 0;
/// This is version of data base that need to up.
/// For example - we has a data base with version 1 and we want to upgrade to version 2 then you need to set cersion field version to 1.
unsigned short versionTo = 0;
/// This is lymbda function with action that will upgrade data base to new versio.
std::function<bool (const QH::iObjectProvider *)> action;
/**
* @brief isValid This method check this oject to valid.
* @return true if object is valid else false.
*/
bool isValid() const;
};
/**
* @brief DBPatchMap This is 2 depch map of the DBPatch structure when the first key it is version (from) and second key is version to.
* @see DataBaseNode::dbPatch
* @see DBPatchMap
*/
typedef QMap<unsigned short, QMap<unsigned short, DBPatch>> DBPatchMap;
}
#endif // DBPATCH_H

@ -51,8 +51,8 @@ bool SqlDBWriter::exec(QSqlQuery *sq, const QString& sqlFile) const {
temp = temp.remove(0, delimiterIndex + 1);
if (!result) {
QuasarAppUtils::Params::log(QString("exec database error. line:%0: %1").
arg(lineNumber).arg(sq->lastError().text()),
QuasarAppUtils::Params::log(QString("Exec database error. File: %0. Line:%1: %2").
arg(sqlFile).arg(lineNumber).arg(sq->lastError().text()),
QuasarAppUtils::Error);
f.close();
return false;
@ -137,10 +137,10 @@ bool SqlDBWriter::doQueryPrivate(const QString &query, QSqlQuery* result) const
return false;
}
QSqlQuery q(query, *db());
if (!q.exec()) {
QuasarAppUtils::Params::log("request error : " + q.lastError().text());
QSqlQuery q(*db());
if (!q.exec(query)) {
QuasarAppUtils::Params::log("request error : " + q.lastError().text(),
QuasarAppUtils::Error);
return false;
}
@ -468,11 +468,12 @@ bool SqlDBWriter::workWithQuery(QSqlQuery &q,
if (!printErrors)
return ;
QuasarAppUtils::Params::log("prepare sql error: " + q.executedQuery(),
QuasarAppUtils::Debug);
QuasarAppUtils::Params::log("exec sql error: " + q.lastError().text(),
QuasarAppUtils::Error);
QuasarAppUtils::Params::log("prepare sql error: " + q.executedQuery(),
QuasarAppUtils::Error);
};

@ -7,6 +7,8 @@
#include <QtTest>
#include <isqldbcache.h>
#include <database.h>
#include <qaglobalutils.h>
#define LOCAL_TEST_PORT TEST_PORT + 5
class UpgradableDatabase: public QH::DataBase {
@ -15,6 +17,10 @@ class UpgradableDatabase: public QH::DataBase {
// DataBaseNode interface
public:
UpgradableDatabase() {
initDBPatches();
}
bool checkVersion(int version) {
QSqlQuery query;
if (!db()->doQuery("SELECT * FROM DataBaseAttributes WHERE name='version'", true, &query)){
@ -31,38 +37,47 @@ public:
protected:
void initDBPatches() {
QH::DBPatchMap dbPatches() const {
QH::DBPatchMap result;
addDBPatch({
0, // from version
1, // to version
result += [](const QH::iObjectProvider* database) -> bool {
QSqlQuery query;
if (!database->doQuery("select * from DataBaseAttributes", true, &query)){
return false;
};
[](const QH::iObjectProvider* database) -> bool {
QSqlQuery query;
if (!database->doQuery("select * from DataBaseAttributes", true, &query)){
return false;
};
return true;
};
return true;
} // action of patch
});
result += [](const QH::iObjectProvider* database) -> bool {
QSqlQuery query;
if (!database->doQuery("select * from DataBaseAttributes", true, &query)){
return false;
};
addDBPatch({
1, // from version
2, // to version
[](const QH::iObjectProvider* database) -> bool {
QSqlQuery query;
if (!database->doQuery("select * from DataBaseAttributes", true, &query)){
return false;
};
return true;
};
return true;
}
});
result += [](const QH::iObjectProvider* database) -> bool {
QSqlQuery query;
if (!database->doQuery("select * from DataBaseAttributes", true, &query)){
return false;
};
addDBPatch({
2, // from version
3, // to version
[](const QH::iObjectProvider* database) -> bool {
QSqlQuery query;
if (!database->doQuery("select * from DataBaseAttributes", true, &query)){
return false;
};
return true;
};
return result;
return true;
}
});
}
};