mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Use controller::User instead of old User Model in SigningTransaction and add some needed functions for that in other objects
This commit is contained in:
parent
b2dc53c899
commit
371d02a800
@ -108,13 +108,12 @@ KeyPairEd25519* KeyPairEd25519::create(const Poco::AutoPtr<Passphrase> passphras
|
||||
//printf("[KeyPair::generateFromPassphrase] finished!\n");
|
||||
// using
|
||||
}
|
||||
|
||||
MemoryBin* KeyPairEd25519::sign(const MemoryBin* message) const
|
||||
MemoryBin* KeyPairEd25519::sign(const unsigned char* message, size_t messageSize) const
|
||||
//MemoryBin* KeyPairEd25519::sign(const MemoryBin* message) const
|
||||
{
|
||||
|
||||
if (!message || !message->size()) return nullptr;
|
||||
if (!message || !messageSize) return nullptr;
|
||||
if (!mSodiumSecret) return nullptr;
|
||||
auto messageSize = message->size();
|
||||
auto mm = MemoryManager::getInstance();
|
||||
auto em = ErrorManager::getInstance();
|
||||
|
||||
@ -123,19 +122,19 @@ MemoryBin* KeyPairEd25519::sign(const MemoryBin* message) const
|
||||
auto signBinBuffer = mm->getFreeMemory(crypto_sign_BYTES);
|
||||
unsigned long long actualSignLength = 0;
|
||||
|
||||
if (crypto_sign_detached(*signBinBuffer, &actualSignLength, *message, messageSize, *mSodiumSecret)) {
|
||||
if (crypto_sign_detached(*signBinBuffer, &actualSignLength, message, messageSize, *mSodiumSecret)) {
|
||||
em->addError(new Error(functionName, "sign failed"));
|
||||
auto messageHex = DataTypeConverter::binToHex(message);
|
||||
auto messageHex = DataTypeConverter::binToHex(message, messageSize);
|
||||
em->addError(new ParamError(functionName, "message as hex", messageHex));
|
||||
mm->releaseMemory(signBinBuffer);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (crypto_sign_verify_detached(*signBinBuffer, *message, messageSize, mSodiumPublic) != 0) {
|
||||
if (crypto_sign_verify_detached(*signBinBuffer, message, messageSize, mSodiumPublic) != 0) {
|
||||
// Incorrect signature!
|
||||
//printf("c[KeyBuffer::%s] sign verify failed\n", __FUNCTION__);
|
||||
em->addError(new Error(functionName, "sign verify failed"));
|
||||
auto messageHex = DataTypeConverter::binToHex(message);
|
||||
auto messageHex = DataTypeConverter::binToHex(message, messageSize);
|
||||
em->addError(new ParamError(functionName, "message as hex", messageHex));
|
||||
mm->releaseMemory(signBinBuffer);
|
||||
return nullptr;
|
||||
|
||||
@ -32,7 +32,9 @@ public:
|
||||
static KeyPairEd25519* create(const Poco::AutoPtr<Passphrase> passphrase);
|
||||
|
||||
//! \return caller take ownership of return value
|
||||
MemoryBin* sign(const MemoryBin* message) const;
|
||||
MemoryBin* sign(const MemoryBin* message) const { return sign(message->data(), message->size()); }
|
||||
inline MemoryBin* sign(const std::string& bodyBytes) const { return sign((const unsigned char*)bodyBytes.data(), bodyBytes.size()); }
|
||||
MemoryBin* sign(const unsigned char* message, size_t messageSize) const;
|
||||
|
||||
inline const unsigned char* getPublicKey() const { return mSodiumPublic; }
|
||||
|
||||
|
||||
@ -159,17 +159,17 @@ namespace DataTypeConverter
|
||||
return base64String;
|
||||
}
|
||||
|
||||
std::string binToHex(const MemoryBin* data)
|
||||
std::string binToHex(const unsigned char* data, size_t size)
|
||||
{
|
||||
auto mm = MemoryManager::getInstance();
|
||||
size_t hexSize = data->size() * 2 + 1;
|
||||
size_t binSize = data->size();
|
||||
size_t hexSize = size * 2 + 1;
|
||||
size_t binSize = size;
|
||||
MemoryBin* hex = mm->getFreeMemory(hexSize);
|
||||
memset(*hex, 0, hexSize);
|
||||
|
||||
size_t resultBinSize = 0;
|
||||
|
||||
sodium_bin2hex(*hex, hexSize, *data, binSize);
|
||||
sodium_bin2hex(*hex, hexSize, data, binSize);
|
||||
|
||||
std::string hexString((const char*)*hex, hexSize);
|
||||
mm->releaseMemory(hex);
|
||||
|
||||
@ -25,7 +25,10 @@ namespace DataTypeConverter {
|
||||
MemoryBin* base64ToBin(const std::string& base64String);
|
||||
|
||||
std::string binToBase64(const MemoryBin* data);
|
||||
std::string binToHex(const MemoryBin* data);
|
||||
|
||||
std::string binToHex(const unsigned char* data, size_t size);
|
||||
inline std::string binToHex(const MemoryBin* data) { return binToHex(data->data(), data->size());}
|
||||
|
||||
//! \param pubkey pointer to array with crypto_sign_PUBLICKEYBYTES size
|
||||
std::string pubkeyToHex(const unsigned char* pubkey);
|
||||
|
||||
|
||||
@ -20,24 +20,40 @@
|
||||
#include "Poco/Net/HTTPRequest.h"
|
||||
#include "Poco/Net/HTTPResponse.h"
|
||||
|
||||
SigningTransaction::SigningTransaction(Poco::AutoPtr<ProcessingTransaction> processingeTransaction, Poco::AutoPtr<User> user)
|
||||
: mProcessingeTransaction(processingeTransaction), mUser(user)
|
||||
SigningTransaction::SigningTransaction(Poco::AutoPtr<ProcessingTransaction> processingeTransaction, Poco::AutoPtr<controller::User> newUser)
|
||||
: mProcessingeTransaction(processingeTransaction), mNewUser(newUser)
|
||||
{
|
||||
auto ob = SingletonTaskObserver::getInstance();
|
||||
if (!mUser.isNull() && mUser->getEmail() != "") {
|
||||
ob->addTask(mUser->getEmail(), TASK_OBSERVER_SIGN_TRANSACTION);
|
||||
auto email = getUserEmail();
|
||||
|
||||
if (email != "") {
|
||||
ob->addTask(email, TASK_OBSERVER_SIGN_TRANSACTION);
|
||||
}
|
||||
}
|
||||
|
||||
SigningTransaction::~SigningTransaction()
|
||||
{
|
||||
auto ob = SingletonTaskObserver::getInstance();
|
||||
//Poco::Thread::sleep(10000);
|
||||
if (!mUser.isNull() && mUser->getEmail() != "") {
|
||||
ob->removeTask(mUser->getEmail(), TASK_OBSERVER_SIGN_TRANSACTION);
|
||||
auto email = getUserEmail();
|
||||
|
||||
if (email != "") {
|
||||
ob->removeTask(email, TASK_OBSERVER_SIGN_TRANSACTION);
|
||||
}
|
||||
}
|
||||
|
||||
std::string SigningTransaction::getUserEmail()
|
||||
{
|
||||
model::table::User* user_model = nullptr;
|
||||
|
||||
if (!mNewUser.isNull()) {
|
||||
user_model = mNewUser->getModel();
|
||||
}
|
||||
if (user_model) {
|
||||
return user_model->getEmail();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
int SigningTransaction::run() {
|
||||
auto mm = MemoryManager::getInstance();
|
||||
|
||||
@ -45,16 +61,17 @@ int SigningTransaction::run() {
|
||||
addError(transactionError, false);
|
||||
|
||||
//= new Error("SigningTransaction start", mProcessingeTransaction->g)
|
||||
if (mUser.isNull() || !mUser->hasCryptoKey()) {
|
||||
//if (mUser.isNull() || !mUser->hasCryptoKey()) {
|
||||
if(mNewUser.isNull() || !mNewUser->hasPassword()) {
|
||||
addError(new Error("SigningTransaction", "user hasn't crypto key or is null"));
|
||||
sendErrorsAsEmail();
|
||||
return -1;
|
||||
}
|
||||
|
||||
//auto privKey = mUser->getPrivKey();
|
||||
if (!mUser->hasPrivKey()) {
|
||||
getErrors(mUser);
|
||||
addError(new Error("SigningTransaction", "couldn't get user priv key"));
|
||||
//if (!mUser->hasPrivKey()) {
|
||||
if(!mNewUser->canDecryptPrivateKey()) {
|
||||
addError(new Error("SigningTransaction", "user cannot decrypt private key"));
|
||||
sendErrorsAsEmail();
|
||||
return -2;
|
||||
}
|
||||
@ -68,14 +85,14 @@ int SigningTransaction::run() {
|
||||
return -3;
|
||||
}
|
||||
// sign
|
||||
auto sign = mUser->sign((const unsigned char*)bodyBytes->data(), bodyBytes->size());
|
||||
//auto sign = mUser->sign((const unsigned char*)bodyBytes->data(), bodyBytes->size());
|
||||
auto sign = mNewUser->getGradidoKeyPair()->sign(*bodyBytes);
|
||||
if (!sign) {
|
||||
getErrors(mUser);
|
||||
ErrorManager::getInstance()->sendErrorsAsEmail();
|
||||
sendErrorsAsEmail();
|
||||
mm->releaseMemory(sign);
|
||||
return -4;
|
||||
}
|
||||
auto pubkeyHex = mUser->getPublicKeyHex();
|
||||
|
||||
// pubkey for signature
|
||||
/*auto pubkeyBin = mm->getFreeMemory(ed25519_pubkey_SIZE);
|
||||
@ -93,7 +110,7 @@ int SigningTransaction::run() {
|
||||
auto sigPair = sigMap->add_sigpair();
|
||||
|
||||
auto pubkeyBytes = sigPair->mutable_pubkey();
|
||||
auto pubkeyBin = mUser->getPublicKey();
|
||||
auto pubkeyBin = mNewUser->getModel()->getPublicKey();
|
||||
*pubkeyBytes = std::string((const char*)pubkeyBin, crypto_sign_PUBLICKEYBYTES);
|
||||
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#include "../lib/ErrorList.h"
|
||||
#include "../model/TransactionBase.h"
|
||||
#include "../model/User.h"
|
||||
#include "../controller/User.h"
|
||||
|
||||
#include "../proto/gradido/Transaction.pb.h"
|
||||
|
||||
@ -21,7 +22,7 @@
|
||||
class SigningTransaction : public UniLib::controller::CPUTask, public ErrorList
|
||||
{
|
||||
public:
|
||||
SigningTransaction(Poco::AutoPtr<ProcessingTransaction> processingeTransaction, Poco::AutoPtr<User> user);
|
||||
SigningTransaction(Poco::AutoPtr<ProcessingTransaction> processingeTransaction, Poco::AutoPtr<controller::User> newUser);
|
||||
virtual ~SigningTransaction();
|
||||
|
||||
int run();
|
||||
@ -32,10 +33,12 @@ public:
|
||||
|
||||
protected:
|
||||
Poco::AutoPtr<ProcessingTransaction> mProcessingeTransaction;
|
||||
Poco::AutoPtr<User> mUser;
|
||||
|
||||
Poco::AutoPtr<controller::User> mNewUser;
|
||||
|
||||
private:
|
||||
|
||||
std::string getUserEmail();
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user