Unverified Commit 4b0ff8a5 authored by kladko's avatar kladko

bug/SKALE-3662 Adding libzmq

parent 0e0dcaad
/*
Copyright (C) 2018-2019 SKALE Labs
This file is part of libBLS.
libBLS is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libBLS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
@file BLSRspSignMessage.h
@author Stan Kladko
@date 2020
*/
#include "BLSSignRspMessage.h"
#include "SGXWalletServer.hpp"
Json::Value BLSSignRspMessage::process() {
assert(false);
}
\ No newline at end of file
/*
Copyright (C) 2018-2019 SKALE Labs
This file is part of libBLS.
libBLS is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libBLS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
@file BLSRspSignMessage.h
@author Stan Kladko
@date 2020
*/
#ifndef SGXWALLET_BLSSIGNRSPMSG_H
#define SGXWALLET_BLSSIGNRSPMSG_H
#include "ZMQMessage.h"
class BLSSignRspMessage : public ZMQMessage {
public:
BLSSignRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
#endif //SGXWALLET_BLSSIGNRSPMSG_H
// /*
// Created by kladko on 15.12.20. Copyright (C) 2018-2019 SKALE Labs
//
This file is part of libBLS.
libBLS is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libBLS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
@file ECDSASignReqMessage.cpp
@author Stan Kladko
@date 2020
*/
#include "SGXWalletServer.hpp" #include "SGXWalletServer.hpp"
......
/* /*
Copyright (C) 2018-2019 SKALE Labs Copyright (C) 2018- SKALE Labs
This file is part of libBLS. This file is part of libBLS.
......
/*
Copyright (C) 2018- SKALE Labs
This file is part of libBLS.
libBLS is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libBLS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
@file ECDSARspSignMessage.cpp
@author Stan Kladko
@date 2020
*/
#include "SGXWalletServer.hpp"
#include "ECDSASignRspMessage.h"
Json::Value ECDSASignRspMessage::process() {
// never called
assert(false);
}
\ No newline at end of file
/*
Copyright (C) 2018- SKALE Labs
This file is part of libBLS.
libBLS is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libBLS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
@file ECDSARspSignMessage.h
@author Stan Kladko
@date 2020
*/
#ifndef SGXWALLET_ECDSASIGNRSPMESSAGE_H
#define SGXWALLET_ECDSASIGNRSPMESSAGE_H
#include "ZMQMessage.h"
class ECDSASignRspMessage : public ZMQMessage {
public:
ECDSASignRspMessage(shared_ptr <rapidjson::Document> &_d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
#endif //SGXWALLET_ECDSASIGNRSPMESSAGE_H
...@@ -70,7 +70,7 @@ bin_PROGRAMS = sgxwallet testw sgx_util ...@@ -70,7 +70,7 @@ bin_PROGRAMS = sgxwallet testw sgx_util
## have to be explicitly listed ## have to be explicitly listed
COMMON_SRC = ECDSASignReqMessage.cpp BLSSignReqMessage.cpp ZMQMessage.cpp ZMQServer.cpp ServerWorker.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp \ COMMON_SRC = BLSSignRspMessage.cpp ECDSASignRspMessage.cpp ECDSASignReqMessage.cpp BLSSignReqMessage.cpp ZMQMessage.cpp ZMQServer.cpp ServerWorker.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp \
SGXWalletServer.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp BLSCrypto.cpp \ SGXWalletServer.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp BLSCrypto.cpp \
DKGCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp ServerDataChecker.cpp SEKManager.cpp \ DKGCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp ServerDataChecker.cpp SEKManager.cpp \
third_party/intel/sgx_stub.c third_party/intel/sgx_detect_linux.c third_party/intel/create_enclave.c third_party/intel/oc_alloc.c \ third_party/intel/sgx_stub.c third_party/intel/sgx_detect_linux.c third_party/intel/create_enclave.c third_party/intel/oc_alloc.c \
......
...@@ -3,7 +3,13 @@ ...@@ -3,7 +3,13 @@
// //
#include "common.h" #include "common.h"
#include <json/writer.h> #include <json/writer.h>
#include <zmq.hpp>
#include "zhelpers.hpp"
#include "ZMQMessage.h" #include "ZMQMessage.h"
#include "ServerWorker.h" #include "ServerWorker.h"
...@@ -22,7 +28,7 @@ void ServerWorker::work() { ...@@ -22,7 +28,7 @@ void ServerWorker::work() {
memcpy(msgData.data(), msg.data(), msg.size()); memcpy(msgData.data(), msg.data(), msg.size());
auto parsedMsg = ZMQMessage::parse(msgData); auto parsedMsg = ZMQMessage::parse(msgData, true);
CHECK_STATE(parsedMsg); CHECK_STATE(parsedMsg);
......
// /*
// Created by skale on 11.01.21. Copyright (C) 2018-2019 SKALE Labs
//
This file is part of skale-consensus.
skale-consensus is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
skale-consensus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with skale-consensus. If not, see <https://www.gnu.org/licenses/>.
@file ZMQClient.cpp
@author Stan Kladko
@date 2020
*/
#include "ZMQClient.h" #include "ZMQClient.h"
// /*
// Created by skale on 11.01.21. Copyright (C) 2018-2019 SKALE Labs
//
This file is part of skale-consensus.
skale-consensus is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
skale-consensus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with skale-consensus. If not, see <https://www.gnu.org/licenses/>.
@file ZMQClient.h
@author Stan Kladko
@date 2021
*/
#ifndef SGXWALLET_ZMQCLIENT_H #ifndef SGXWALLET_ZMQCLIENT_H
#define SGXWALLET_ZMQCLIENT_H #define SGXWALLET_ZMQCLIENT_H
#include "third_party/spdlog/spdlog.h"
#include <zmq.hpp>
#include "zhelpers.hpp"
#include <jsonrpccpp/client.h> #include <jsonrpccpp/client.h>
#include "ZMQMessage.h" #include "ZMQMessage.h"
#define REQUEST_TIMEOUT 2500 // msecs, (> 1000!) #define REQUEST_TIMEOUT 2500 // msecs, (> 1000!)
class ZMQClient { class ZMQClient {
ZMQClient(string &ip, uint16_t port) : ctx(1), private:
clientSocket(ctx_, ZMQ_REQ) {
url = "tcp://" + ip + ":" + to_string(port);
}
void reconnect() {
clientSocket = nullptr; // delete previous
clientSocket = make_unique < zmq::socket_t > clientSocket(ctx_, ZMQ_REQ);
clienSocket->connect(url);
// Configure socket to not wait at close time
int linger = 0;
clientSocket->setsockopt(ZMQ_LINGER, &linger, sizeof(linger));
}
Json::Value blsSignMessageHash(const std::string &keyShareName, const std::string &messageHash, int t, int n) { zmq::context_t ctx;
Json::Value p; std::unique_ptr <zmq::socket_t> clientSocket;
p["type"] = ZMQMessage::BLS_SIGN_REQ; string url;
p["keyShareName"] = keyShareName;
p["messageHash"] = messageHash;
p["n"] = n;
p["t"] = t;
Json::Value result = sendRequest(p);
if (result.isObject())
return result;
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
shared_ptr<ZMQMessage> doRequestReply(Json::Value &_req) { shared_ptr <ZMQMessage> doRequestReply(Json::Value &_req) {
Json::FastWriter fastWriter; Json::FastWriter fastWriter;
string reqStr = fastWriter.write(_req); string reqStr = fastWriter.write(_req);
auto resultStr = doZmqRequestReply(reqStr); auto resultStr = doZmqRequestReply(reqStr);
return ZMQMessage::parse(resultStr); return ZMQMessage::parse(resultStr.c_str(), resultStr.size(), false);
} }
string doZmqRequestReply(string &_req) { string doZmqRequestReply(string &_req) {
stringstream request; stringstream request;
s_send(*client, _req.str()); s_send(*clientSocket, _req);
while (true) { while (true) {
// Poll socket for a reply, with timeout // Poll socket for a reply, with timeout
zmq::pollitem_t items[] = { zmq::pollitem_t items[] = {
{static_cast<void *>(*client), 0, ZMQ_POLLIN, 0}}; {static_cast<void *>(*clientSocket), 0, ZMQ_POLLIN, 0}};
zmq::poll(&items[0], 1, REQUEST_TIMEOUT); zmq::poll(&items[0], 1, REQUEST_TIMEOUT);
// If we got a reply, process it // If we got a reply, process it
if (items[0].revents & ZMQ_POLLIN) { if (items[0].revents & ZMQ_POLLIN) {
reply = s_recv(*client); string reply = s_recv(*clientSocket);
return reply; return reply;
} else { } else {
spdlog::error("W: no response from server, retrying..."); spdlog::error("W: no response from server, retrying...");
reconnect(); reconnect();
// Send request again, on new socket // Send request again, on new socket
s_send(*client, _req.str()); s_send(*clientSocket, _req);
} }
} }
} }
}; public:
Json::Value ecdsaSignMessageHash(int base, const std::string &keyName, const std::string &messageHash) {
Json::Value p;
p["type"] = ZMQMessage::ECDSA_SIGN_REQ;
p["base"] = base;
p["keyName"] = keyName;
p["messageHash"] = messageHash;
Json::Value result = sendRequest(p);
if (result.isObject())
return result;
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
private: ZMQClient(string &ip, uint16_t port) : ctx(1) {
url = "tcp://" + ip + ":" + to_string(port);
}
void reconnect() {
clientSocket = nullptr; // delete previous
clientSocket = make_unique<zmq::socket_t>(ctx, ZMQ_REQ);
clientSocket->connect(url);
// Configure socket to not wait at close time
int linger = 0;
clientSocket->setsockopt(ZMQ_LINGER, &linger, sizeof(linger));
}
string blsSignMessageHash(const std::string &keyShareName, const std::string &messageHash, int t, int n) {
Json::Value p;
p["type"] = ZMQMessage::BLS_SIGN_REQ;
p["keyShareName"] = keyShareName;
p["messageHash"] = messageHash;
p["n"] = n;
p["t"] = t;
auto result = doRequestReply(p);
return "";
}
string ecdsaSignMessageHash(int base, const std::string &keyName, const std::string &messageHash) {
Json::Value p;
p["type"] = ZMQMessage::ECDSA_SIGN_REQ;
p["base"] = base;
p["keyName"] = keyName;
p["messageHash"] = messageHash;
auto result = doRequestReply(p);
return "";
}
zmq::context_t ctx;
unique_ptr <zmq::socket_t> clientSocket;
string url;
}; };
#endif //SGXWALLET_ZMQCLIENT_H #endif //SGXWALLET_ZMQCLIENT_H
...@@ -24,7 +24,9 @@ ...@@ -24,7 +24,9 @@
#include "common.h" #include "common.h"
#include "BLSSignReqMessage.h" #include "BLSSignReqMessage.h"
#include "BLSSignRspMessage.h"
#include "ECDSASignReqMessage.h" #include "ECDSASignReqMessage.h"
#include "ECDSASignRspMessage.h"
#include "ZMQMessage.h" #include "ZMQMessage.h"
...@@ -43,52 +45,62 @@ string ZMQMessage::getStringRapid(const char *_name) { ...@@ -43,52 +45,62 @@ string ZMQMessage::getStringRapid(const char *_name) {
return (*d)[_name].GetString(); return (*d)[_name].GetString();
}; };
shared_ptr <ZMQMessage> ZMQMessage::parse(vector <uint8_t> &_msg, bool _isRequest) { shared_ptr <ZMQMessage> ZMQMessage::parse(vector <uint8_t> &_msg, bool _isRequest) {
CHECK_STATE(_msg.at(_msg.size() - 1) == 0); return parse((const char *) _msg.data(), _msg.size(), _isRequest);
}
shared_ptr <ZMQMessage> ZMQMessage::parse(const char* _msg,
size_t _size, bool _isRequest) {
CHECK_STATE(_size > 0);
CHECK_STATE(_msg);
// CHECK NULL TERMINATED
CHECK_STATE(_msg[_size - 1] == 0);
auto d = make_shared<rapidjson::Document>(); auto d = make_shared<rapidjson::Document>();
d->Parse((const char *) _msg.data()); d->Parse(_msg);
CHECK_STATE(!d->HasParseError()); CHECK_STATE(!d->HasParseError());
CHECK_STATE(d->IsObject()) CHECK_STATE(d->IsObject())
CHECK_STATE(d->HasMember("type")); CHECK_STATE(d->HasMember("type"));
CHECK_STATE((*d)["type"].IsString()); CHECK_STATE((*d)["type"].IsString());
auto type = (*d)["type"].GetString(); string type = (*d)["type"].GetString();
shared_ptr <ZMQMessage> result; shared_ptr <ZMQMessage> result;
if (isRequest) { if (_isRequest) {
return buildRequest(type, d); return buildRequest(type, d);
} else { } else {
return buildResponse(type, d); return buildResponse(type, d);
} }
} }
shared_ptr <ZMQMessage> ZMQMessage::buildRequest(string type, shared_ptr <rapidjson::Document> _d) { shared_ptr <ZMQMessage> ZMQMessage::buildRequest(string& _type, shared_ptr<rapidjson::Document> _d) {
if (type == ZMQMessage::BLS_SIGN_REQ) { if (_type == ZMQMessage::BLS_SIGN_REQ) {
return return make_shared<BLSSignReqMessage>(_d);
make_shared<BLSSignReqMessage>(d); } else if (_type == ZMQMessage::ECDSA_SIGN_REQ) {
} else if (type == ZMQMessage::ECDSA_SIGN_REQ) {
return return
make_shared<ECDSASignReqMessage>(d); make_shared<ECDSASignReqMessage>(_d);
} else { } else {
BOOST_THROW_EXCEPTION((-301, "Incorrect zmq message type: " + BOOST_THROW_EXCEPTION(SGXException(-301, "Incorrect zmq message type: " +
string(type))); string(_type)));
} }
} }
shared_ptr <ZMQMessage> ZMQMessage::buildRequest(string& type, shared_ptr <rapidjson::Document> _d) { shared_ptr <ZMQMessage> ZMQMessage::buildResponse(string& _type, shared_ptr<rapidjson::Document> _d) {
if (type == ZMQMessage::BLS_SIGN_RSP) { if (_type == ZMQMessage::BLS_SIGN_RSP) {
return return
make_shared<BLSSignRspMessage>(d); make_shared<BLSSignRspMessage>(_d);
} else if (type == ZMQMessage::ECDSA_SIGN_REQ) { } else if (_type == ZMQMessage::ECDSA_SIGN_REQ) {
return return
make_shared<ECDSASignRspMessage>(d); make_shared<ECDSASignRspMessage>(_d);
} else { } else {
BOOST_THROW_EXCEPTION(InvalidStateException("Incorrect zmq message request type: " + string(type), BOOST_THROW_EXCEPTION(InvalidStateException("Incorrect zmq message request type: " + string(_type),
__CLASS_NAME__) __CLASS_NAME__)
); );
} }
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
#include <vector> #include <vector>
#include "abstractstubserver.h" #include "abstractstubserver.h"
#include "document.h" #include "document.h"
#include "SGXException.h" #include "SGXException.h"
...@@ -58,9 +57,10 @@ public: ...@@ -58,9 +57,10 @@ public:
uint64_t getUint64Rapid(const char *_name); uint64_t getUint64Rapid(const char *_name);
static shared_ptr<ZMQMessage> parse(vector<uint8_t> &_msg, bool _isRequest); static shared_ptr<ZMQMessage> parse(vector<uint8_t> &_msg, bool _isRequest);
static shared_ptr <ZMQMessage> parse(const char* _msg, size_t _size, bool _isRequest);
shared_ptr<ZMQMessage> buildRequest(string& type, shared_ptr <rapidjson::Document> _d); static shared_ptr<ZMQMessage> buildRequest(string& type, shared_ptr<rapidjson::Document> _d);
shared_ptr<ZMQMessage> buildResponse(string& type, shared_ptr <rapidjson::Document> _d); static shared_ptr<ZMQMessage> buildResponse(string& type, shared_ptr<rapidjson::Document> _d);
virtual Json::Value process() = 0; virtual Json::Value process() = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment