Unverified Commit d8ed5870 authored by Stan Kladko's avatar Stan Kladko Committed by GitHub

Merge pull request #138 from skalenetwork/SKALE-3067-cleanup-improve-sgx

Skale 3067 cleanup improve sgx
parents 20362fb3 6c340277
......@@ -73,11 +73,30 @@ public:
static void handleSGXException(Json::Value &_result, SGXException &_e);
};
#define INIT_RESULT(__RESULT__) Json::Value __RESULT__; __RESULT__["status"] = 0; __RESULT__["errorMessage"] = \
#define INIT_RESULT(__RESULT__) Json::Value __RESULT__; \
int errStatus = UNKNOWN_ERROR; string errMsg(BUF_LEN, '\0');__RESULT__["status"] = 0; __RESULT__["errorMessage"] = \
"Server error. Please see server log.";
#define RESULT_SUCCESS(__RESULT__) ; __RESULT__["status"] = 0; __RESULT__["errorMessage"] = "";
#define HANDLE_SGX_EXCEPTION(_RESULT_) catch (SGXException &__e) { Log::handleSGXException(_RESULT_, __e);} \
catch (exception &__e) {spdlog::error(__e.what()); _RESULT_["status"] = 1; _RESULT_["errorMessage"] = __e.what();}
#define HANDLE_SGX_EXCEPTION(__RESULT__) \
catch (SGXException& _e) { \
__RESULT__["status"] = _e.status; \
__RESULT__["errorMessage"] = _e.errString; \
return __RESULT__; \
} catch (exception& _e) { \
__RESULT__["errorMessage"] = _e.what(); \
return __RESULT__; \
}\
catch (...) { \
exception_ptr p = current_exception(); \
printf("Exception %s \n", p.__cxa_exception_type()->name()); \
__RESULT__["errorMessage"] = "Unknown exception"; \
return __RESULT__; \
}
#define RETURN_SUCCESS(__RESULT__) \
__RESULT__["status"] = 0; \
__RESULT__["errorMessage"] = ""; \
return __RESULT__;
#endif
......@@ -135,7 +135,7 @@ int SGXWalletServer::initHttpsServer(bool _checkCerts) {
if (system(genCert.c_str()) == 0) {
spdlog::info("SERVER CERTIFICATE IS SUCCESSFULLY GENERATED");
} else {
spdlog::info("SERVER CERTIFICATE GENERATION FAILED");
spdlog::error("SERVER CERTIFICATE GENERATION FAILED");
exit(-1);
}
}
......@@ -166,19 +166,13 @@ 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');
result["status"] = 0;
result["errorMessage"] = "";
INIT_RESULT(result);
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());
......@@ -188,26 +182,25 @@ SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_k
throw SGXException(UNKNOWN_ERROR, "");
}
writeKeyShare(_keyShareName, encryptedKeyShareHex, _index, n, t);
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;
result["status"] = -1;
result["errorMessage"] = "Unknown server error";
INIT_RESULT(result);
result["signatureShare"] = "";
string signature(BUF_LEN, '\0');
vector<char> signature(BUF_LEN, 0);
shared_ptr <string> value = nullptr;
......@@ -232,45 +225,45 @@ SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const strin
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
return result;
} catch (...) {
} catch (exception& _e) {
result["errorMessage"] = _e.what();
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";
result["errorMessage"] = "Exception in dbRead";
return result;
}
try {
if (!bls_sign(value->c_str(), _messageHash.c_str(), t, n, _signerIndex, &signature.front())) {
if (!bls_sign(value->c_str(), _messageHash.c_str(), t, n, _signerIndex, signature.data())) {
result["status"] = -1;
result["errorMessage"] = "Could not sign";
return result;
}
} catch (...) {
result["status"] = -1;
result["errorMessage"] = "Sign has thrown exception";
return result;
}
} HANDLE_SGX_EXCEPTION(result);
result["signatureShare"] = string(signature.data());
RETURN_SUCCESS(result)
auto it = signature.find('\0');
result["status"] = 0;
result["errorMessage"] = "";
result["signatureShare"] = std::string(signature.begin(), signature.begin() + it);
return 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;
......@@ -295,11 +288,12 @@ 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) {
INIT_RESULT(result)
result["encryptedKey"] = "";
try {
......@@ -322,7 +316,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) {
......@@ -339,9 +333,6 @@ Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_
if (hashTmp[0] == '0' && (hashTmp[1] == 'x' || hashTmp[1] == 'X')) {
hashTmp.erase(hashTmp.begin(), hashTmp.begin() + 2);
}
// while (hashTmp[0] == '0') {
// hashTmp.erase(hashTmp.begin(), hashTmp.begin() + 1);
// }
if (!checkECDSAKeyName(_keyName)) {
throw SGXException(INVALID_ECDSA_KEY_NAME, "Invalid ECDSA key name");
......@@ -367,7 +358,7 @@ Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_
result["signature_s"] = signatureVector.at(2);
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result)
}
Json::Value SGXWalletServer::getPublicECDSAKeyImpl(const string &_keyName) {
......@@ -391,7 +382,7 @@ Json::Value SGXWalletServer::getPublicECDSAKeyImpl(const string &_keyName) {
result["publicKey"] = publicKey;
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result)
}
Json::Value SGXWalletServer::generateDKGPolyImpl(const string &_polyName, int _t) {
......@@ -411,11 +402,13 @@ 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;
INIT_RESULT(result);
result["status"] = 0;
result["errorMessage"] = "";
......@@ -445,11 +438,14 @@ Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName,
result["verificationVector"] = "";
}
return result;
}
Json::Value SGXWalletServer::getSecretShareImpl(const string &_polyName, const Json::Value &_pubKeys, int _t, int _n) {
Json::Value result;
INIT_RESULT(result);
result["status"] = 0;
result["errorMessage"] = "";
......@@ -476,22 +472,15 @@ 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"] = "";
}
} 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)) {
......@@ -510,21 +499,24 @@ 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;
throw SGXException(INVALID_DKG_PARAMS, "DKG shares did not verify");
}
} catch (SGXException &_e) {
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
result["result"] = false;
}
} HANDLE_SGX_EXCEPTION(result)
return result;
result["result"] = true;
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;
INIT_RESULT(result)
result["status"] = 0;
result["errorMessage"] = "";
......@@ -569,7 +561,7 @@ SGXWalletServer::createBLSPrivateKeyImpl(const string &_blsKeyName, const string
} HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result)
}
Json::Value SGXWalletServer::getBLSPublicKeyShareImpl(const string &_blsKeyName) {
......@@ -589,7 +581,8 @@ 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 +601,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,6 +614,9 @@ Json::Value SGXWalletServer::multG2Impl(const string &_x) {
}
} HANDLE_SGX_EXCEPTION(result)
result["status"] = 0;
result["errorMessage"] = "";
return result;
}
......@@ -637,17 +633,22 @@ Json::Value SGXWalletServer::isPolyExistsImpl(const string &_polyName) {
}
} HANDLE_SGX_EXCEPTION(result)
result["status"] = 0;
result["errorMessage"] = "";
return 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);
result["status"] = 0;
result["errorMessage"] = "";
return result;
}
......@@ -670,7 +671,8 @@ Json::Value SGXWalletServer::deleteBlsKeyImpl(const std::string& 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) {
......
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