Unverified Commit 0637814a authored by Stan Kladko's avatar Stan Kladko Committed by GitHub

Merge pull request #139 from skalenetwork/SKALE-3067-revert-bad-commits

SKALE-3067-cleanup-sgx
parents d8ed5870 57ef6392
...@@ -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::error("SERVER CERTIFICATE GENERATION FAILED"); spdlog::info("SERVER CERTIFICATE GENERATION FAILED");
exit(-1); exit(-1);
} }
} }
...@@ -166,13 +166,19 @@ int SGXWalletServer::initHttpServer() { //without ssl ...@@ -166,13 +166,19 @@ 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) {
INIT_RESULT(result); Json::Value 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, (char*) errMsg.data(), _keyShare.c_str()); encryptedKeyShareHex = encryptBLSKeyShare2Hex(&errStatus, &errMsg.front(), _keyShare.c_str());
if (errStatus != 0) { if (errStatus != 0) {
throw SGXException(errStatus, errMsg.data()); throw SGXException(errStatus, errMsg.data());
...@@ -182,25 +188,26 @@ SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_k ...@@ -182,25 +188,26 @@ 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;
} HANDLE_SGX_EXCEPTION(result) writeKeyShare(_keyShareName, encryptedKeyShareHex, _index, n, t);
} catch (SGXException &_e) {
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
}
RETURN_SUCCESS(result) return 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;
INIT_RESULT(result); result["status"] = -1;
result["errorMessage"] = "Unknown server error";
result["signatureShare"] = ""; result["signatureShare"] = "";
vector<char> signature(BUF_LEN, 0); string signature(BUF_LEN, '\0');
shared_ptr <string> value = nullptr; shared_ptr <string> value = nullptr;
...@@ -225,45 +232,45 @@ SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const strin ...@@ -225,45 +232,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 (exception& _e) { } catch (...) {
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["errorMessage"] = "Exception in dbRead"; result["status"] = -1;
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.data())) { if (!bls_sign(value->c_str(), _messageHash.c_str(), t, n, _signerIndex, &signature.front())) {
result["status"] = -1; result["status"] = -1;
result["errorMessage"] = "Could not sign"; result["errorMessage"] = "Could not sign";
return result; return result;
} }
} HANDLE_SGX_EXCEPTION(result); } catch (...) {
result["status"] = -1;
result["errorMessage"] = "Sign has thrown exception";
result["signatureShare"] = string(signature.data()); return result;
}
RETURN_SUCCESS(result)
auto it = std::find(signature.begin(), signature.end(), '\0');
result["status"] = 0;
result["errorMessage"] = "";
result["signatureShare"] = std::string(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;
INIT_RESULT(result); result["status"] = 0;
result["errorMessage"] = "";
result["encryptedKey"] = ""; result["encryptedKey"] = "";
RETURN_SUCCESS(result) return result;
} }
Json::Value SGXWalletServer::generateECDSAKeyImpl() { Json::Value SGXWalletServer::generateECDSAKeyImpl() {
Json::Value result;
INIT_RESULT(result); result["status"] = 0;
result["errorMessage"] = "";
result["encryptedKey"] = ""; result["encryptedKey"] = "";
vector <string> keys; vector <string> keys;
...@@ -288,12 +295,11 @@ Json::Value SGXWalletServer::generateECDSAKeyImpl() { ...@@ -288,12 +295,11 @@ Json::Value SGXWalletServer::generateECDSAKeyImpl() {
result["keyName"] = keyName; result["keyName"] = keyName;
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
RETURN_SUCCESS(result) return 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 {
...@@ -316,7 +322,7 @@ Json::Value SGXWalletServer::renameECDSAKeyImpl(const string &_keyName, const st ...@@ -316,7 +322,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_SUCCESS(result) return 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) {
...@@ -326,13 +332,16 @@ Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_ ...@@ -326,13 +332,16 @@ Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_
result["signature_r"] = ""; result["signature_r"] = "";
result["signature_s"] = ""; result["signature_s"] = "";
vector<string> signatureVector(3); vector <string> signatureVector(3);
try { try {
string hashTmp = _messageHash; string hashTmp = _messageHash;
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");
...@@ -344,9 +353,9 @@ Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_ ...@@ -344,9 +353,9 @@ Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_
throw SGXException(-22, "Invalid base"); throw SGXException(-22, "Invalid base");
} }
shared_ptr<string> encryptedKey = readFromDb(_keyName); shared_ptr <string> encryptedKey = readFromDb(_keyName, "");
signatureVector = ecdsaSignHash(*encryptedKey, hashTmp.c_str(), _base); signatureVector = ecdsaSignHash(encryptedKey->c_str(), hashTmp.c_str(), _base);
if (signatureVector.size() != 3) { if (signatureVector.size() != 3) {
throw SGXException(INVALID_ECSDA_SIGNATURE, "Invalid ecdsa signature"); throw SGXException(INVALID_ECSDA_SIGNATURE, "Invalid ecdsa signature");
} }
...@@ -358,7 +367,7 @@ Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_ ...@@ -358,7 +367,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_SUCCESS(result) return result;
} }
Json::Value SGXWalletServer::getPublicECDSAKeyImpl(const string &_keyName) { Json::Value SGXWalletServer::getPublicECDSAKeyImpl(const string &_keyName) {
...@@ -382,7 +391,7 @@ Json::Value SGXWalletServer::getPublicECDSAKeyImpl(const string &_keyName) { ...@@ -382,7 +391,7 @@ Json::Value SGXWalletServer::getPublicECDSAKeyImpl(const string &_keyName) {
result["publicKey"] = publicKey; result["publicKey"] = publicKey;
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
RETURN_SUCCESS(result) return result;
} }
Json::Value SGXWalletServer::generateDKGPolyImpl(const string &_polyName, int _t) { Json::Value SGXWalletServer::generateDKGPolyImpl(const string &_polyName, int _t) {
...@@ -402,13 +411,11 @@ Json::Value SGXWalletServer::generateDKGPolyImpl(const string &_polyName, int _t ...@@ -402,13 +411,11 @@ Json::Value SGXWalletServer::generateDKGPolyImpl(const string &_polyName, int _t
writeDataToDB(_polyName, encrPolyHex); writeDataToDB(_polyName, encrPolyHex);
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
RETURN_SUCCESS(result) return 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"] = "";
...@@ -438,14 +445,11 @@ Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName, ...@@ -438,14 +445,11 @@ 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"] = "";
...@@ -472,15 +476,22 @@ Json::Value SGXWalletServer::getSecretShareImpl(const string &_polyName, const J ...@@ -472,15 +476,22 @@ 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;
} HANDLE_SGX_EXCEPTION(result) } catch (SGXException &_e) {
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
result["secretShare"] = "";
result["SecretShare"] = "";
}
RETURN_SUCCESS(result) return 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) {
INIT_RESULT(result) Json::Value result;
result["result"] = false; result["status"] = 0;
result["errorMessage"] = "";
result["result"] = true;
try { try {
if (!checkECDSAKeyName(_ethKeyName)) { if (!checkECDSAKeyName(_ethKeyName)) {
...@@ -499,24 +510,21 @@ Json::Value SGXWalletServer::dkgVerificationImpl(const string &_publicShares, co ...@@ -499,24 +510,21 @@ 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)) {
throw SGXException(INVALID_DKG_PARAMS, "DKG shares did not verify"); result["result"] = false;
} }
} HANDLE_SGX_EXCEPTION(result) } catch (SGXException &_e) {
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
result["result"] = false;
result["result"] = true; }
RETURN_SUCCESS(result) return 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"] = "";
...@@ -538,11 +546,11 @@ SGXWalletServer::createBLSPrivateKeyImpl(const string &_blsKeyName, const string ...@@ -538,11 +546,11 @@ SGXWalletServer::createBLSPrivateKeyImpl(const string &_blsKeyName, const string
if (!check_n_t(_t, _n)) { if (!check_n_t(_t, _n)) {
throw SGXException(INVALID_DKG_PARAMS, "Invalid DKG parameters: n or t "); 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); 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()); bool res = CreateBLSShare(_blsKeyName, _secretShare.c_str(), encryptedKeyHex_ptr->c_str());
if (res) { if (res) {
...@@ -561,7 +569,7 @@ SGXWalletServer::createBLSPrivateKeyImpl(const string &_blsKeyName, const string ...@@ -561,7 +569,7 @@ SGXWalletServer::createBLSPrivateKeyImpl(const string &_blsKeyName, const string
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
RETURN_SUCCESS(result) return result;
} }
Json::Value SGXWalletServer::getBLSPublicKeyShareImpl(const string &_blsKeyName) { Json::Value SGXWalletServer::getBLSPublicKeyShareImpl(const string &_blsKeyName) {
...@@ -571,18 +579,17 @@ Json::Value SGXWalletServer::getBLSPublicKeyShareImpl(const string &_blsKeyName) ...@@ -571,18 +579,17 @@ Json::Value SGXWalletServer::getBLSPublicKeyShareImpl(const string &_blsKeyName)
if (!checkName(_blsKeyName, "BLS_KEY")) { if (!checkName(_blsKeyName, "BLS_KEY")) {
throw SGXException(INVALID_BLS_NAME, "Invalid BLSKey name"); throw SGXException(INVALID_BLS_NAME, "Invalid BLSKey name");
} }
shared_ptr<string> encryptedKeyHex_ptr = readFromDb(_blsKeyName); shared_ptr <string> encryptedKeyHex_ptr = readFromDb(_blsKeyName);
spdlog::debug("encr_bls_key_share is {}", *encryptedKeyHex_ptr); spdlog::debug("encr_bls_key_share is {}", *encryptedKeyHex_ptr);
spdlog::debug("length is {}", encryptedKeyHex_ptr->length()); spdlog::debug("length is {}", encryptedKeyHex_ptr->length());
vector<string> public_key_vect = GetBLSPubKey(encryptedKeyHex_ptr->c_str()); vector <string> public_key_vect = GetBLSPubKey(encryptedKeyHex_ptr->c_str());
for (uint8_t i = 0; i < 4; i++) { for (uint8_t i = 0; i < 4; i++) {
result["blsPublicKeyShare"][i] = public_key_vect.at(i); result["blsPublicKeyShare"][i] = public_key_vect.at(i);
} }
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
RETURN_SUCCESS(result) return result;
} }
Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int _ind) { Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int _ind) {
...@@ -601,7 +608,7 @@ Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int ...@@ -601,7 +608,7 @@ Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int
result["dhKey"] = DHKey; result["dhKey"] = DHKey;
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
RETURN_SUCCESS(result) return result;
} }
Json::Value SGXWalletServer::multG2Impl(const string &_x) { Json::Value SGXWalletServer::multG2Impl(const string &_x) {
...@@ -614,9 +621,6 @@ Json::Value SGXWalletServer::multG2Impl(const string &_x) { ...@@ -614,9 +621,6 @@ Json::Value SGXWalletServer::multG2Impl(const string &_x) {
} }
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
result["status"] = 0;
result["errorMessage"] = "";
return result; return result;
} }
...@@ -633,22 +637,17 @@ Json::Value SGXWalletServer::isPolyExistsImpl(const string &_polyName) { ...@@ -633,22 +637,17 @@ 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_SUCCESS(result) return 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;
} }
...@@ -671,8 +670,7 @@ Json::Value SGXWalletServer::deleteBlsKeyImpl(const std::string& name) { ...@@ -671,8 +670,7 @@ 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) {
...@@ -778,9 +776,17 @@ void SGXWalletServer::writeKeyShare(const string &_keyShareName, const string &_ ...@@ -778,9 +776,17 @@ void SGXWalletServer::writeKeyShare(const string &_keyShareName, const string &_
LevelDB::getLevelDb()->writeString(_keyShareName, _value); LevelDB::getLevelDb()->writeString(_keyShareName, _value);
} }
void SGXWalletServer::writeDataToDB(const string &key, const string &value) { void SGXWalletServer::writeDataToDB(const string &Name, const string &value) {
if (LevelDB::getLevelDb()->readString(key) != nullptr) { Json::Value val;
spdlog::info("name {}", key, " already exists"); Json::FastWriter writer;
val["value"] = value;
writer.write(val);
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_SHARE_ALREADY_EXISTS, "Key share already exists");
} }
......
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