Unverified Commit 6f35e65a authored by Stan Kladko's avatar Stan Kladko Committed by GitHub

Merge pull request #141 from skalenetwork/SKALE-3067-cleanip2

Skale 3067 cleanip2
parents b95ab32d f3456f30
......@@ -40,7 +40,6 @@ CSRManagerServer::CSRManagerServer(AbstractServerConnector &connector,
serverVersion_t type) : abstractCSRManagerServer(connector, type) {}
Json::Value getUnsignedCSRsImpl() {
spdlog::info(__FUNCTION__);
INIT_RESULT(result)
try {
......@@ -50,12 +49,11 @@ Json::Value getUnsignedCSRsImpl() {
}
} HANDLE_SGX_EXCEPTION(result);
return result;
RETURN_SUCCESS(result)
}
Json::Value signByHashImpl(const string &hash, int status) {
Json::Value result;
result["errorMessage"] = "";
INIT_RESULT(result)
try {
if (!(status == 0 || status == 2)) {
......@@ -89,7 +87,6 @@ Json::Value signByHashImpl(const string &hash, int status) {
LevelDB::getCsrStatusDb()->deleteKey(status_db_key);
LevelDB::getCsrStatusDb()->writeDataUnique(status_db_key, "-1");
throw SGXException(FAIL_TO_CREATE_CERTIFICATE, "CLIENT CERTIFICATE GENERATION FAILED");
//exit(-1);
}
}
......@@ -102,7 +99,7 @@ Json::Value signByHashImpl(const string &hash, int status) {
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result)
}
Json::Value CSRManagerServer::getUnsignedCSRs() {
......
......@@ -43,15 +43,10 @@ static WriteOptions writeOptions;
static ReadOptions readOptions;
std::shared_ptr<string> LevelDB::readString(const string &_key) {
std::lock_guard<std::recursive_mutex> lock(mutex);
auto result = std::make_shared<string>();
if (db == nullptr) {
throw SGXException(NULL_DATABASE, "Null db");
}
spdlog::debug("key to read from db: {}", _key);
CHECK_STATE(db)
auto status = db->Get(readOptions, _key, result.get());
......@@ -65,18 +60,14 @@ std::shared_ptr<string> LevelDB::readString(const string &_key) {
}
void LevelDB::writeString(const string &_key, const string &_value) {
std::lock_guard<std::recursive_mutex> lock(mutex);
auto status = db->Put(writeOptions, Slice(_key), Slice(_value));
throwExceptionOnError(status);
spdlog::debug("written key: {}", _key);
}
void LevelDB::deleteDHDKGKey(const string &_key) {
std::lock_guard<std::recursive_mutex> lock(mutex);
string full_key = "DKG_DH_KEY_" + _key;
......@@ -84,48 +75,31 @@ void LevelDB::deleteDHDKGKey(const string &_key) {
throwExceptionOnError(status);
spdlog::debug("key deleted: {}", full_key);
}
void LevelDB::deleteTempNEK(const string &_key) {
std::lock_guard<std::recursive_mutex> lock(mutex);
string prefix = _key.substr(0,8);
if (prefix != "tmp_NEK:") {
return;
}
CHECK_STATE(_key.rfind("tmp_NEK", 0) == 0);
auto status = db->Delete(writeOptions, Slice(_key));
throwExceptionOnError(status);
spdlog::debug("key deleted: {}", _key);
}
void LevelDB::deleteKey(const string &_key) {
std::lock_guard<std::recursive_mutex> lock(mutex);
auto status = db->Delete(writeOptions, Slice(_key));
throwExceptionOnError(status);
spdlog::debug("key deleted: {}", _key);
}
void LevelDB::writeByteArray(const char *_key, size_t _keyLen, const char *value,
size_t _valueLen) {
std::lock_guard<std::recursive_mutex> lock(mutex);
auto status = db->Put(writeOptions, Slice(_key, _keyLen), Slice(value, _valueLen));
throwExceptionOnError(status);
}
void LevelDB::writeByteArray(string &_key, const char *value,
size_t _valueLen) {
std::lock_guard<std::recursive_mutex> lock(mutex);
CHECK_STATE(value);
auto status = db->Put(writeOptions, Slice(_key), Slice(value, _valueLen));
......@@ -142,6 +116,9 @@ void LevelDB::throwExceptionOnError(Status _status) {
}
uint64_t LevelDB::visitKeys(LevelDB::KeyVisitor *_visitor, uint64_t _maxKeysToVisit) {
CHECK_STATE(_visitor);
uint64_t readCounter = 0;
leveldb::Iterator *it = db->NewIterator(readOptions);
......@@ -187,7 +164,7 @@ void LevelDB::writeDataUnique(const string & Name, const string &value) {
writeString(key, value);
spdlog::debug("{}",Name, " is written to db");
}
......
......@@ -74,7 +74,7 @@ public:
};
#define INIT_RESULT(__RESULT__) Json::Value __RESULT__; \
int errStatus = UNKNOWN_ERROR; string errMsg(BUF_LEN, '\0');__RESULT__["status"] = 0; __RESULT__["errorMessage"] = \
int errStatus = UNKNOWN_ERROR; string errMsg(BUF_LEN, '\0');__RESULT__["status"] = UNKNOWN_ERROR; __RESULT__["errorMessage"] = \
"Server error. Please see server log.";
#define HANDLE_SGX_EXCEPTION(__RESULT__) \
......
......@@ -112,7 +112,7 @@ Json::Value signCertificateImpl(const string &_csr, bool _autoSign = false) {
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result)
}
Json::Value getCertificateImpl(const string &hash) {
......@@ -147,7 +147,7 @@ Json::Value getCertificateImpl(const string &hash) {
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result)
}
......
......@@ -85,8 +85,8 @@ bool isStringDec(const string &_str) {
return !_str.empty() && res == _str.end();
}
shared_ptr<SGXWalletServer> SGXWalletServer::server = nullptr;
shared_ptr<HttpServer> SGXWalletServer::httpServer = nullptr;
shared_ptr <SGXWalletServer> SGXWalletServer::server = nullptr;
shared_ptr <HttpServer> SGXWalletServer::httpServer = nullptr;
SGXWalletServer::SGXWalletServer(AbstractServerConnector &_connector,
serverVersion_t _type)
......@@ -166,19 +166,15 @@ int SGXWalletServer::initHttpServer() { //without ssl
Json::Value
SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_keyShareName, int t, int n, int _index) {
Json::Value result;
int errStatus = UNKNOWN_ERROR;
string errMsg(BUF_LEN, '\0');
INIT_RESULT(result);
result["status"] = 0;
result["errorMessage"] = "";
result["encryptedKeyShare"] = "";
string encryptedKeyShareHex;
try {
encryptedKeyShareHex = encryptBLSKeyShare2Hex(&errStatus, &errMsg.front(), _keyShare.c_str());
encryptedKeyShareHex = encryptBLSKeyShare2Hex(&errStatus, (char *) errMsg.data(), _keyShare.c_str());
if (errStatus != 0) {
throw SGXException(errStatus, errMsg.data());
......@@ -191,23 +187,22 @@ SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_k
result["encryptedKeyShare"] = encryptedKeyShareHex;
writeKeyShare(_keyShareName, encryptedKeyShareHex, _index, n, t);
} catch (SGXException &_e) {
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
}
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result);
}
Json::Value
SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const string &_messageHash, int t, int n,
int _signerIndex) {
Json::Value result;
INIT_RESULT(result)
result["status"] = -1;
result["errorMessage"] = "Unknown server error";
result["signatureShare"] = "";
string signature(BUF_LEN, '\0');
vector<char> signature(BUF_LEN, 0);
shared_ptr <string> value = nullptr;
......@@ -228,49 +223,26 @@ SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const strin
}
value = readFromDb(_keyShareName);
} catch (SGXException& _e) {
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
return result;
} catch (...) {
exception_ptr p = current_exception();
printf("Exception %s \n", p.__cxa_exception_type()->name());
result["status"] = -1;
result["errorMessage"] = "Read key share has thrown exception:";
return result;
if (!bls_sign(value->c_str(), _messageHash.c_str(), t, n, _signerIndex, signature.data())) {
throw SGXException(-1, "Could not sign data ");
}
} HANDLE_SGX_EXCEPTION(result)
try {
if (!bls_sign(value->c_str(), _messageHash.c_str(), t, n, _signerIndex, &signature.front())) {
result["status"] = -1;
result["errorMessage"] = "Could not sign";
return result;
}
} catch (...) {
result["status"] = -1;
result["errorMessage"] = "Sign has thrown exception";
return result;
}
auto it = std::find(signature.begin(), signature.end(), '\0');
result["status"] = 0;
result["errorMessage"] = "";
result["signatureShare"] = std::string(signature.begin(), it);
return result;
result["signatureShare"] = string(signature.data());
RETURN_SUCCESS(result);
}
Json::Value SGXWalletServer::importECDSAKeyImpl(const string &_key, const string &_keyName) {
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
INIT_RESULT(result)
result["encryptedKey"] = "";
return result;
RETURN_SUCCESS(result)
}
Json::Value SGXWalletServer::generateECDSAKeyImpl() {
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
INIT_RESULT(result)
result["encryptedKey"] = "";
vector <string> keys;
......@@ -284,9 +256,6 @@ Json::Value SGXWalletServer::generateECDSAKeyImpl() {
string keyName = "NEK:" + keys.at(2);
spdlog::debug("key name generated: {}", keyName);
spdlog::debug("write encr key {}", keys.at(0));
writeDataToDB(keyName, keys.at(0));
result["encryptedKey"] = keys.at(0);
......@@ -295,7 +264,7 @@ Json::Value SGXWalletServer::generateECDSAKeyImpl() {
result["keyName"] = keyName;
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result);
}
Json::Value SGXWalletServer::renameECDSAKeyImpl(const string &_keyName, const string &_tempKeyName) {
......@@ -322,7 +291,7 @@ Json::Value SGXWalletServer::renameECDSAKeyImpl(const string &_keyName, const st
LevelDB::getLevelDb()->deleteTempNEK(_tempKeyName);
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result);
}
Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_keyName, const string &_messageHash) {
......@@ -360,14 +329,13 @@ Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_
throw SGXException(INVALID_ECSDA_SIGNATURE, "Invalid ecdsa signature");
}
spdlog::debug("got signature_s {}", signatureVector.at(2));
result["signature_v"] = signatureVector.at(0);
result["signature_r"] = signatureVector.at(1);
result["signature_s"] = signatureVector.at(2);
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result)
}
Json::Value SGXWalletServer::getPublicECDSAKeyImpl(const string &_keyName) {
......@@ -384,14 +352,11 @@ Json::Value SGXWalletServer::getPublicECDSAKeyImpl(const string &_keyName) {
}
shared_ptr <string> keyStr = readFromDb(_keyName);
publicKey = getECDSAPubKey(keyStr->c_str());
spdlog::debug("PublicKey {}", publicKey);
spdlog::debug("PublicKey length {}", publicKey.length());
result["PublicKey"] = publicKey;
result["publicKey"] = publicKey;
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result)
}
Json::Value SGXWalletServer::generateDKGPolyImpl(const string &_polyName, int _t) {
......@@ -411,13 +376,11 @@ Json::Value SGXWalletServer::generateDKGPolyImpl(const string &_polyName, int _t
writeDataToDB(_polyName, encrPolyHex);
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result)
}
Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName, int _t, int _n) {
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
INIT_RESULT(result)
vector <vector<string>> verifVector;
try {
......@@ -428,7 +391,7 @@ Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName,
throw SGXException(INVALID_DKG_PARAMS, "Invalid parameters: n or t ");
}
shared_ptr<string> encrPoly = readFromDb(_polyName);
shared_ptr <string> encrPoly = readFromDb(_polyName);
verifVector = get_verif_vect(encrPoly->c_str(), _t, _n);
......@@ -438,20 +401,16 @@ Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName,
result["verificationVector"][i][j] = currentCoef.at(j);
}
}
} catch (SGXException &_e) {
cerr << " err str " << _e.errString << endl;
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
result["verificationVector"] = "";
}
} HANDLE_SGX_EXCEPTION(result)
RETURN_SUCCESS(result)
return result;
}
Json::Value SGXWalletServer::getSecretShareImpl(const string &_polyName, const Json::Value &_pubKeys, int _t, int _n) {
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
INIT_RESULT(result);
result["secretShare"] = "";
result["SecretShare"] = "";
try {
if (_pubKeys.size() != (uint64_t) _n) {
......@@ -476,22 +435,16 @@ Json::Value SGXWalletServer::getSecretShareImpl(const string &_polyName, const J
string s = trustedGetSecretShares(_polyName, encrPoly->c_str(), pubKeysStrs, _t, _n);
result["secretShare"] = s;
} catch (SGXException &_e) {
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
result["secretShare"] = "";
result["SecretShare"] = "";
}
result["SecretShare"] = s;
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result)
}
Json::Value SGXWalletServer::dkgVerificationImpl(const string &_publicShares, const string &_ethKeyName,
const string &_secretShare, int _t, int _n, int _index) {
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
result["result"] = true;
INIT_RESULT(result)
result["result"] = false;
try {
if (!checkECDSAKeyName(_ethKeyName)) {
......@@ -509,29 +462,21 @@ Json::Value SGXWalletServer::dkgVerificationImpl(const string &_publicShares, co
shared_ptr <string> encryptedKeyHex_ptr = readFromDb(_ethKeyName);
if (!verifyShares(_publicShares.c_str(), _secretShare.c_str(), encryptedKeyHex_ptr->c_str(), _t, _n, _index)) {
result["result"] = false;
}
} catch (SGXException &_e) {
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
result["result"] = false;
if (verifyShares(_publicShares.c_str(), _secretShare.c_str(), encryptedKeyHex_ptr->c_str(), _t, _n, _index)) {
result["result"] = true;
}
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result)
}
Json::Value
SGXWalletServer::createBLSPrivateKeyImpl(const string &_blsKeyName, const string &_ethKeyName, const string &_polyName,
const string &_secretShare, int _t, int _n) {
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
INIT_RESULT(result)
try {
if (_secretShare.length() != (uint64_t) _n * 192) {
spdlog::error("Invalid secret share length - {}", _secretShare.length());
spdlog::error("Secret share - {}", _secretShare);
throw SGXException(INVALID_SECRET_SHARES_LENGTH, "Invalid secret share length");
}
if (!checkECDSAKeyName(_ethKeyName)) {
......@@ -546,11 +491,11 @@ SGXWalletServer::createBLSPrivateKeyImpl(const string &_blsKeyName, const string
if (!check_n_t(_t, _n)) {
throw SGXException(INVALID_DKG_PARAMS, "Invalid DKG parameters: n or t ");
}
vector< string > sshares_vect;
vector <string> sshares_vect;
spdlog::debug("secret shares from json are - {}", _secretShare);
shared_ptr< string > encryptedKeyHex_ptr = readFromDb(_ethKeyName);
shared_ptr <string> encryptedKeyHex_ptr = readFromDb(_ethKeyName);
bool res = CreateBLSShare(_blsKeyName, _secretShare.c_str(), encryptedKeyHex_ptr->c_str());
if (res) {
......@@ -569,7 +514,7 @@ SGXWalletServer::createBLSPrivateKeyImpl(const string &_blsKeyName, const string
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result);
}
Json::Value SGXWalletServer::getBLSPublicKeyShareImpl(const string &_blsKeyName) {
......@@ -580,8 +525,6 @@ Json::Value SGXWalletServer::getBLSPublicKeyShareImpl(const string &_blsKeyName)
throw SGXException(INVALID_BLS_NAME, "Invalid BLSKey name");
}
shared_ptr <string> encryptedKeyHex_ptr = readFromDb(_blsKeyName);
spdlog::debug("encr_bls_key_share is {}", *encryptedKeyHex_ptr);
spdlog::debug("length is {}", encryptedKeyHex_ptr->length());
vector <string> public_key_vect = GetBLSPubKey(encryptedKeyHex_ptr->c_str());
for (uint8_t i = 0; i < 4; i++) {
......@@ -589,7 +532,7 @@ Json::Value SGXWalletServer::getBLSPublicKeyShareImpl(const string &_blsKeyName)
}
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result);
}
Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int _ind) {
......@@ -608,7 +551,7 @@ Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int
result["dhKey"] = DHKey;
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result);
}
Json::Value SGXWalletServer::multG2Impl(const string &_x) {
......@@ -621,7 +564,7 @@ Json::Value SGXWalletServer::multG2Impl(const string &_x) {
}
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result);
}
Json::Value SGXWalletServer::isPolyExistsImpl(const string &_polyName) {
......@@ -630,28 +573,28 @@ Json::Value SGXWalletServer::isPolyExistsImpl(const string &_polyName) {
result["IsExist"] = false;
try {
std::shared_ptr <std::string> poly_str_ptr = LevelDB::getLevelDb()->readString(_polyName);
shared_ptr <string> poly_str_ptr = LevelDB::getLevelDb()->readString(_polyName);
if (poly_str_ptr != nullptr) {
result["IsExist"] = true;
}
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result);
}
Json::Value SGXWalletServer::getServerStatusImpl() {
INIT_RESULT(result)
return result;
RETURN_SUCCESS(result)
}
Json::Value SGXWalletServer::getServerVersionImpl() {
INIT_RESULT(result)
result["version"] = TOSTRING(SGXWALLET_VERSION);
return result;
RETURN_SUCCESS(result)
}
Json::Value SGXWalletServer::deleteBlsKeyImpl(const std::string& name) {
Json::Value SGXWalletServer::deleteBlsKeyImpl(const string &name) {
INIT_RESULT(result)
result["deleted"] = false;
......@@ -659,18 +602,18 @@ Json::Value SGXWalletServer::deleteBlsKeyImpl(const std::string& name) {
if (!checkName(name, "BLS_KEY")) {
throw SGXException(INVALID_BLS_NAME, "Invalid BLSKey name format");
}
std::shared_ptr <std::string> bls_ptr = LevelDB::getLevelDb()->readString(name);
shared_ptr <string> bls_ptr = LevelDB::getLevelDb()->readString(name);
if (bls_ptr != nullptr) {
LevelDB::getLevelDb()->deleteKey(name);
result["deleted"] = true;
return result;
} else {
std::string error_msg = "BLS key with such name not found: " + name;
auto error_msg = "BLS key not found: " + name;
throw SGXException(INVALID_BLS_NAME, error_msg.c_str());
}
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result)
}
Json::Value SGXWalletServer::generateDKGPoly(const string &_polyName, int _t) {
......@@ -715,7 +658,6 @@ Json::Value SGXWalletServer::getPublicECDSAKey(const string &_keyName) {
}
Json::Value SGXWalletServer::ecdsaSignMessageHash(int _base, const string &_keyShareName, const string &_messageHash) {
spdlog::debug("MessageHash first {}", _messageHash);
return ecdsaSignMessageHashImpl(_base, _keyShareName, _messageHash);
}
......@@ -754,7 +696,7 @@ Json::Value SGXWalletServer::getServerVersion() {
return getServerVersionImpl();
}
Json::Value SGXWalletServer::deleteBlsKey(const std::string& name) {
Json::Value SGXWalletServer::deleteBlsKey(const string &name) {
return deleteBlsKeyImpl(name);
}
......@@ -786,8 +728,7 @@ void SGXWalletServer::writeDataToDB(const string &Name, const string &value) {
auto key = Name;
if (LevelDB::getLevelDb()->readString(Name) != nullptr) {
spdlog::info("name {}", Name, " already exists");
throw SGXException(KEY_SHARE_ALREADY_EXISTS, "Key share already exists");
throw SGXException(KEY_NAME_ALREADY_EXISTS, "Name already exists");
}
LevelDB::getLevelDb()->writeString(key, value);
......
......@@ -24,10 +24,9 @@
#ifndef SGXWALLET_SGXWALLETSERVER_HPP
#define SGXWALLET_SGXWALLETSERVER_HPP
#include <boost/thread/shared_mutex.hpp>
#include <jsonrpccpp/server/connectors/httpserver.h>
#include <mutex>
#include "abstractstubserver.h"
using namespace jsonrpc;
......
......@@ -174,8 +174,10 @@ void TestUtils::sendRPCRequest() {
vector <string> pubShares(n);
vector <string> polyNames(n);
int schainID = randGen();
int dkgID = randGen();
static atomic<int> counter(1);
int schainID = counter.fetch_add(1);
int dkgID = counter.fetch_add(1);
for (uint8_t i = 0; i < n; i++) {
ethKeys[i] = c.generateECDSAKey();
CHECK_STATE(ethKeys[i]["status"] == 0);
......
......@@ -3,6 +3,8 @@
#define ECDSA_ENCR_LEN 93
#define ECDSA_BIN_LEN 33
#define SMALL_BUF_SIZE 1024
#define TINY_BUF_SIZE 256
enclave {
trusted {
......
......@@ -89,6 +89,8 @@ extern int autoconfirm;
#define INVALID_ECDSA_KEY_NAME -20
#define INVALID_HEX -21
#define INVALID_ECSDA_SIGNATURE -22
#define KEY_NAME_ALREADY_EXISTS -23 \
#define ERROR_IN_ENCLAVE -33
......
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