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: ...@@ -73,11 +73,30 @@ public:
static void handleSGXException(Json::Value &_result, SGXException &_e); 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."; "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);} \ #define HANDLE_SGX_EXCEPTION(__RESULT__) \
catch (exception &__e) {spdlog::error(__e.what()); _RESULT_["status"] = 1; _RESULT_["errorMessage"] = __e.what();} 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 #endif
...@@ -135,7 +135,7 @@ int SGXWalletServer::initHttpsServer(bool _checkCerts) { ...@@ -135,7 +135,7 @@ int SGXWalletServer::initHttpsServer(bool _checkCerts) {
if (system(genCert.c_str()) == 0) { if (system(genCert.c_str()) == 0) {
spdlog::info("SERVER CERTIFICATE IS SUCCESSFULLY GENERATED"); spdlog::info("SERVER CERTIFICATE IS SUCCESSFULLY GENERATED");
} else { } else {
spdlog::info("SERVER CERTIFICATE GENERATION FAILED"); spdlog::error("SERVER CERTIFICATE GENERATION FAILED");
exit(-1); exit(-1);
} }
} }
...@@ -166,19 +166,13 @@ int SGXWalletServer::initHttpServer() { //without ssl ...@@ -166,19 +166,13 @@ int SGXWalletServer::initHttpServer() { //without ssl
Json::Value Json::Value
SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_keyShareName, int t, int n, int _index) { SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_keyShareName, int t, int n, int _index) {
Json::Value result; INIT_RESULT(result);
int errStatus = UNKNOWN_ERROR;
string errMsg(BUF_LEN, '\0');
result["status"] = 0;
result["errorMessage"] = "";
result["encryptedKeyShare"] = ""; result["encryptedKeyShare"] = "";
string encryptedKeyShareHex; string encryptedKeyShareHex;
try { try {
encryptedKeyShareHex = encryptBLSKeyShare2Hex(&errStatus, &errMsg.front(), _keyShare.c_str()); encryptedKeyShareHex = encryptBLSKeyShare2Hex(&errStatus, (char*) errMsg.data(), _keyShare.c_str());
if (errStatus != 0) { if (errStatus != 0) {
throw SGXException(errStatus, errMsg.data()); throw SGXException(errStatus, errMsg.data());
...@@ -188,26 +182,25 @@ SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_k ...@@ -188,26 +182,25 @@ SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_k
throw SGXException(UNKNOWN_ERROR, ""); throw SGXException(UNKNOWN_ERROR, "");
} }
writeKeyShare(_keyShareName, encryptedKeyShareHex, _index, n, t);
result["encryptedKeyShare"] = encryptedKeyShareHex; result["encryptedKeyShare"] = encryptedKeyShareHex;
writeKeyShare(_keyShareName, encryptedKeyShareHex, _index, n, t); } HANDLE_SGX_EXCEPTION(result)
} catch (SGXException &_e) {
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
}
return result;
RETURN_SUCCESS(result)
} }
Json::Value Json::Value
SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const string &_messageHash, int t, int n, SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const string &_messageHash, int t, int n,
int _signerIndex) { int _signerIndex) {
Json::Value result;
result["status"] = -1; INIT_RESULT(result);
result["errorMessage"] = "Unknown server error";
result["signatureShare"] = ""; result["signatureShare"] = "";
string signature(BUF_LEN, '\0'); vector<char> signature(BUF_LEN, 0);
shared_ptr <string> value = nullptr; shared_ptr <string> value = nullptr;
...@@ -232,45 +225,45 @@ SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const strin ...@@ -232,45 +225,45 @@ SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const strin
result["status"] = _e.status; result["status"] = _e.status;
result["errorMessage"] = _e.errString; result["errorMessage"] = _e.errString;
return result; return result;
} catch (...) { } catch (exception& _e) {
result["errorMessage"] = _e.what();
return result;
}
catch (...) {
exception_ptr p = current_exception(); exception_ptr p = current_exception();
printf("Exception %s \n", p.__cxa_exception_type()->name()); printf("Exception %s \n", p.__cxa_exception_type()->name());
result["status"] = -1; result["errorMessage"] = "Exception in dbRead";
result["errorMessage"] = "Read key share has thrown exception";
return result; return result;
} }
try { 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["status"] = -1;
result["errorMessage"] = "Could not sign"; result["errorMessage"] = "Could not sign";
return result; return result;
} }
} catch (...) { } HANDLE_SGX_EXCEPTION(result);
result["status"] = -1;
result["errorMessage"] = "Sign has thrown exception";
return 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 SGXWalletServer::importECDSAKeyImpl(const string &_key, const string &_keyName) {
Json::Value result;
result["status"] = 0; INIT_RESULT(result);
result["errorMessage"] = "";
result["encryptedKey"] = ""; result["encryptedKey"] = "";
return result; RETURN_SUCCESS(result)
} }
Json::Value SGXWalletServer::generateECDSAKeyImpl() { Json::Value SGXWalletServer::generateECDSAKeyImpl() {
Json::Value result;
result["status"] = 0; INIT_RESULT(result);
result["errorMessage"] = "";
result["encryptedKey"] = ""; result["encryptedKey"] = "";
vector <string> keys; vector <string> keys;
...@@ -295,11 +288,12 @@ Json::Value SGXWalletServer::generateECDSAKeyImpl() { ...@@ -295,11 +288,12 @@ Json::Value SGXWalletServer::generateECDSAKeyImpl() {
result["keyName"] = keyName; result["keyName"] = keyName;
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
return result; RETURN_SUCCESS(result)
} }
Json::Value SGXWalletServer::renameECDSAKeyImpl(const string &_keyName, const string &_tempKeyName) { Json::Value SGXWalletServer::renameECDSAKeyImpl(const string &_keyName, const string &_tempKeyName) {
INIT_RESULT(result) INIT_RESULT(result)
result["encryptedKey"] = ""; result["encryptedKey"] = "";
try { try {
...@@ -322,7 +316,7 @@ Json::Value SGXWalletServer::renameECDSAKeyImpl(const string &_keyName, const st ...@@ -322,7 +316,7 @@ Json::Value SGXWalletServer::renameECDSAKeyImpl(const string &_keyName, const st
LevelDB::getLevelDb()->deleteTempNEK(_tempKeyName); LevelDB::getLevelDb()->deleteTempNEK(_tempKeyName);
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
return result; RETURN_SUCCESS(result)
} }
Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_keyName, const string &_messageHash) { Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_keyName, const string &_messageHash) {
...@@ -339,9 +333,6 @@ Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_ ...@@ -339,9 +333,6 @@ Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_
if (hashTmp[0] == '0' && (hashTmp[1] == 'x' || hashTmp[1] == 'X')) { if (hashTmp[0] == '0' && (hashTmp[1] == 'x' || hashTmp[1] == 'X')) {
hashTmp.erase(hashTmp.begin(), hashTmp.begin() + 2); hashTmp.erase(hashTmp.begin(), hashTmp.begin() + 2);
} }
// while (hashTmp[0] == '0') {
// hashTmp.erase(hashTmp.begin(), hashTmp.begin() + 1);
// }
if (!checkECDSAKeyName(_keyName)) { if (!checkECDSAKeyName(_keyName)) {
throw SGXException(INVALID_ECDSA_KEY_NAME, "Invalid ECDSA key name"); throw SGXException(INVALID_ECDSA_KEY_NAME, "Invalid ECDSA key name");
...@@ -367,7 +358,7 @@ Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_ ...@@ -367,7 +358,7 @@ Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_
result["signature_s"] = signatureVector.at(2); result["signature_s"] = signatureVector.at(2);
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
return result; RETURN_SUCCESS(result)
} }
Json::Value SGXWalletServer::getPublicECDSAKeyImpl(const string &_keyName) { Json::Value SGXWalletServer::getPublicECDSAKeyImpl(const string &_keyName) {
...@@ -391,7 +382,7 @@ Json::Value SGXWalletServer::getPublicECDSAKeyImpl(const string &_keyName) { ...@@ -391,7 +382,7 @@ Json::Value SGXWalletServer::getPublicECDSAKeyImpl(const string &_keyName) {
result["publicKey"] = publicKey; result["publicKey"] = publicKey;
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
return result; RETURN_SUCCESS(result)
} }
Json::Value SGXWalletServer::generateDKGPolyImpl(const string &_polyName, int _t) { Json::Value SGXWalletServer::generateDKGPolyImpl(const string &_polyName, int _t) {
...@@ -411,11 +402,13 @@ 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); writeDataToDB(_polyName, encrPolyHex);
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
return result; RETURN_SUCCESS(result)
} }
Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName, int _t, int _n) { Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName, int _t, int _n) {
Json::Value result;
INIT_RESULT(result);
result["status"] = 0; result["status"] = 0;
result["errorMessage"] = ""; result["errorMessage"] = "";
...@@ -445,11 +438,14 @@ Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName, ...@@ -445,11 +438,14 @@ Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName,
result["verificationVector"] = ""; result["verificationVector"] = "";
} }
return result; return result;
} }
Json::Value SGXWalletServer::getSecretShareImpl(const string &_polyName, const Json::Value &_pubKeys, int _t, int _n) { 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["status"] = 0;
result["errorMessage"] = ""; result["errorMessage"] = "";
...@@ -476,22 +472,15 @@ Json::Value SGXWalletServer::getSecretShareImpl(const string &_polyName, const J ...@@ -476,22 +472,15 @@ Json::Value SGXWalletServer::getSecretShareImpl(const string &_polyName, const J
string s = trustedGetSecretShares(_polyName, encrPoly->c_str(), pubKeysStrs, _t, _n); string s = trustedGetSecretShares(_polyName, encrPoly->c_str(), pubKeysStrs, _t, _n);
result["secretShare"] = s; result["secretShare"] = s;
} catch (SGXException &_e) { } HANDLE_SGX_EXCEPTION(result)
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
result["secretShare"] = "";
result["SecretShare"] = "";
}
return result; RETURN_SUCCESS(result)
} }
Json::Value SGXWalletServer::dkgVerificationImpl(const string &_publicShares, const string &_ethKeyName, Json::Value SGXWalletServer::dkgVerificationImpl(const string &_publicShares, const string &_ethKeyName,
const string &_secretShare, int _t, int _n, int _index) { const string &_secretShare, int _t, int _n, int _index) {
Json::Value result; INIT_RESULT(result)
result["status"] = 0; result["result"] = false;
result["errorMessage"] = "";
result["result"] = true;
try { try {
if (!checkECDSAKeyName(_ethKeyName)) { if (!checkECDSAKeyName(_ethKeyName)) {
...@@ -510,21 +499,24 @@ Json::Value SGXWalletServer::dkgVerificationImpl(const string &_publicShares, co ...@@ -510,21 +499,24 @@ Json::Value SGXWalletServer::dkgVerificationImpl(const string &_publicShares, co
shared_ptr <string> encryptedKeyHex_ptr = readFromDb(_ethKeyName); shared_ptr <string> encryptedKeyHex_ptr = readFromDb(_ethKeyName);
if (!verifyShares(_publicShares.c_str(), _secretShare.c_str(), encryptedKeyHex_ptr->c_str(), _t, _n, _index)) { 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) { } HANDLE_SGX_EXCEPTION(result)
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
result["result"] = false;
}
return result;
result["result"] = true;
RETURN_SUCCESS(result)
} }
Json::Value Json::Value
SGXWalletServer::createBLSPrivateKeyImpl(const string &_blsKeyName, const string &_ethKeyName, const string &_polyName, SGXWalletServer::createBLSPrivateKeyImpl(const string &_blsKeyName, const string &_ethKeyName, const string &_polyName,
const string &_secretShare, int _t, int _n) { const string &_secretShare, int _t, int _n) {
Json::Value result;
INIT_RESULT(result)
result["status"] = 0; result["status"] = 0;
result["errorMessage"] = ""; result["errorMessage"] = "";
...@@ -569,7 +561,7 @@ SGXWalletServer::createBLSPrivateKeyImpl(const string &_blsKeyName, const string ...@@ -569,7 +561,7 @@ SGXWalletServer::createBLSPrivateKeyImpl(const string &_blsKeyName, const string
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
return result; RETURN_SUCCESS(result)
} }
Json::Value SGXWalletServer::getBLSPublicKeyShareImpl(const string &_blsKeyName) { Json::Value SGXWalletServer::getBLSPublicKeyShareImpl(const string &_blsKeyName) {
...@@ -589,7 +581,8 @@ Json::Value SGXWalletServer::getBLSPublicKeyShareImpl(const string &_blsKeyName) ...@@ -589,7 +581,8 @@ Json::Value SGXWalletServer::getBLSPublicKeyShareImpl(const string &_blsKeyName)
} }
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
return result; RETURN_SUCCESS(result)
} }
Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int _ind) { Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int _ind) {
...@@ -608,7 +601,7 @@ Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int ...@@ -608,7 +601,7 @@ Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int
result["dhKey"] = DHKey; result["dhKey"] = DHKey;
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
return result; RETURN_SUCCESS(result)
} }
Json::Value SGXWalletServer::multG2Impl(const string &_x) { Json::Value SGXWalletServer::multG2Impl(const string &_x) {
...@@ -621,6 +614,9 @@ Json::Value SGXWalletServer::multG2Impl(const string &_x) { ...@@ -621,6 +614,9 @@ Json::Value SGXWalletServer::multG2Impl(const string &_x) {
} }
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
result["status"] = 0;
result["errorMessage"] = "";
return result; return result;
} }
...@@ -637,17 +633,22 @@ Json::Value SGXWalletServer::isPolyExistsImpl(const string &_polyName) { ...@@ -637,17 +633,22 @@ Json::Value SGXWalletServer::isPolyExistsImpl(const string &_polyName) {
} }
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
result["status"] = 0;
result["errorMessage"] = "";
return result; return result;
} }
Json::Value SGXWalletServer::getServerStatusImpl() { Json::Value SGXWalletServer::getServerStatusImpl() {
INIT_RESULT(result) INIT_RESULT(result)
return result; RETURN_SUCCESS(result)
} }
Json::Value SGXWalletServer::getServerVersionImpl() { Json::Value SGXWalletServer::getServerVersionImpl() {
INIT_RESULT(result) INIT_RESULT(result)
result["version"] = TOSTRING(SGXWALLET_VERSION); result["version"] = TOSTRING(SGXWALLET_VERSION);
result["status"] = 0;
result["errorMessage"] = "";
return result; return result;
} }
...@@ -670,7 +671,8 @@ Json::Value SGXWalletServer::deleteBlsKeyImpl(const std::string& name) { ...@@ -670,7 +671,8 @@ Json::Value SGXWalletServer::deleteBlsKeyImpl(const std::string& name) {
throw SGXException(INVALID_BLS_NAME, error_msg.c_str()); throw SGXException(INVALID_BLS_NAME, error_msg.c_str());
} }
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
return result;
RETURN_SUCCESS(result)
} }
Json::Value SGXWalletServer::generateDKGPoly(const string &_polyName, int _t) { 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