Unverified Commit 93b8db50 authored by svetaro's avatar svetaro

SKALE-1779 Add key name correctness check

parent d3d3523c
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include <gmp.h> #include <gmp.h>
#include <random> #include <random>
static std::default_random_engine rand_gen((unsigned int) time(0));
std::vector<std::string> gen_ecdsa_key(){ std::vector<std::string> gen_ecdsa_key(){
char *errMsg = (char *)calloc(1024, 1); char *errMsg = (char *)calloc(1024, 1);
int err_status = 0; int err_status = 0;
...@@ -28,7 +30,7 @@ std::vector<std::string> gen_ecdsa_key(){ ...@@ -28,7 +30,7 @@ std::vector<std::string> gen_ecdsa_key(){
//std::cerr << "in ECDSACrypto encr key x " << keys.at(0) << std::endl; //std::cerr << "in ECDSACrypto encr key x " << keys.at(0) << std::endl;
//std::cerr << "in ECDSACrypto encr_len %d " << enc_len << std::endl; //std::cerr << "in ECDSACrypto encr_len %d " << enc_len << std::endl;
std::default_random_engine rand_gen((unsigned int) time(0));
unsigned long seed = rand_gen(); unsigned long seed = rand_gen();
std::cerr << "seed is " << seed << std::endl; std::cerr << "seed is " << seed << std::endl;
gmp_randstate_t state; gmp_randstate_t state;
......
...@@ -29,6 +29,19 @@ ...@@ -29,6 +29,19 @@
#include "SGXWalletServer.h" #include "SGXWalletServer.h"
#include "SGXWalletServer.hpp" #include "SGXWalletServer.hpp"
#include <algorithm>
bool isStringDec( std::string & str){
auto res = std::find_if_not(str.begin(), str.end(), [](char c)->bool{
return std::isdigit(c);
});
return !str.empty() && res == str.end();
// bool res =tr
// for (int i = 0; i < str.length; i++){
// }
}
SGXWalletServer::SGXWalletServer(AbstractServerConnector &connector, SGXWalletServer::SGXWalletServer(AbstractServerConnector &connector,
serverVersion_t type) serverVersion_t type)
: AbstractStubServer(connector, type) {} : AbstractStubServer(connector, type) {}
...@@ -179,7 +192,7 @@ Json::Value generateECDSAKeyImpl() { ...@@ -179,7 +192,7 @@ Json::Value generateECDSAKeyImpl() {
return result; return result;
} }
Json::Value renameESDSAKeyImpl(const std::string& KeyName, const std::string& tempKeyName){ Json::Value renameECDSAKeyImpl(const std::string& KeyName, const std::string& tempKeyName){
Json::Value result; Json::Value result;
result["status"] = 0; result["status"] = 0;
result["errorMessage"] = ""; result["errorMessage"] = "";
...@@ -189,14 +202,19 @@ Json::Value renameESDSAKeyImpl(const std::string& KeyName, const std::string& te ...@@ -189,14 +202,19 @@ Json::Value renameESDSAKeyImpl(const std::string& KeyName, const std::string& te
std::string prefix = tempKeyName.substr(0,8); std::string prefix = tempKeyName.substr(0,8);
if (prefix != "tmp_NEK:") { if (prefix != "tmp_NEK:") {
throw RPCException(UNKNOWN_ERROR, ""); throw RPCException(UNKNOWN_ERROR, "wrong temp key name");
} }
prefix = KeyName.substr(0,5); prefix = KeyName.substr(0,12);
if (prefix != "NEK_NODE_ID:") { if (prefix != "NEK_NODE_ID:") {
throw RPCException(UNKNOWN_ERROR, ""); throw RPCException(UNKNOWN_ERROR, "wrong key name");
}
std::string postfix = KeyName.substr(12, KeyName.length());
if (!isStringDec(postfix)){
throw RPCException(UNKNOWN_ERROR, "wrong key name");
} }
std::shared_ptr<std::string> key_ptr = readFromDb(tempKeyName); std::shared_ptr<std::string> key_ptr = readFromDb(tempKeyName);
std::cerr << "new key name is " << KeyName <<std::endl;
writeDataToDB(KeyName, *key_ptr); writeDataToDB(KeyName, *key_ptr);
levelDb->deleteTempNEK(tempKeyName); levelDb->deleteTempNEK(tempKeyName);
...@@ -417,7 +435,7 @@ Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::s ...@@ -417,7 +435,7 @@ Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::s
//std::cerr << sshares << std::endl; //std::cerr << sshares << std::endl;
//std::cerr << "length is " << strlen(sshares); //std::cerr << "length is " << strlen(sshares);
std::shared_ptr<std::string> encryptedKeyHex_ptr = readFromDb(EthKeyName);//readECDSAKey(EthKeyName); std::shared_ptr<std::string> encryptedKeyHex_ptr = readFromDb(EthKeyName);
bool res = CreateBLSShare(BLSKeyName, sshares, encryptedKeyHex_ptr->c_str()); bool res = CreateBLSShare(BLSKeyName, sshares, encryptedKeyHex_ptr->c_str());
if ( res){ if ( res){
...@@ -523,9 +541,9 @@ Json::Value SGXWalletServer::generateECDSAKey() { ...@@ -523,9 +541,9 @@ Json::Value SGXWalletServer::generateECDSAKey() {
return generateECDSAKeyImpl(); return generateECDSAKeyImpl();
} }
Json::Value SGXWalletServer::renameESDSAKey(const std::string& KeyName, const std::string& tempKeyName){ Json::Value SGXWalletServer::renameECDSAKey(const std::string& KeyName, const std::string& tempKeyName){
lock_guard<recursive_mutex> lock(m); lock_guard<recursive_mutex> lock(m);
return renameESDSAKeyImpl(KeyName, tempKeyName); return renameECDSAKeyImpl(KeyName, tempKeyName);
} }
Json::Value SGXWalletServer::getPublicECDSAKey(const std::string &_keyName) { Json::Value SGXWalletServer::getPublicECDSAKey(const std::string &_keyName) {
...@@ -668,3 +686,4 @@ void writeDataToDB(const string & Name, const string &value) { ...@@ -668,3 +686,4 @@ void writeDataToDB(const string & Name, const string &value) {
levelDb->writeString(key, value); levelDb->writeString(key, value);
} }
...@@ -25,7 +25,7 @@ public: ...@@ -25,7 +25,7 @@ public:
virtual Json::Value importECDSAKey(const std::string& key, const std::string& keyName); virtual Json::Value importECDSAKey(const std::string& key, const std::string& keyName);
virtual Json::Value generateECDSAKey(); virtual Json::Value generateECDSAKey();
virtual Json::Value renameESDSAKey(const std::string& KeyName, const std::string& tempKeyName); virtual Json::Value renameECDSAKey(const std::string& KeyName, const std::string& tempKeyName);
virtual Json::Value ecdsaSignMessageHash(int base, const std::string& keyShareName, const std::string& messageHash); virtual Json::Value ecdsaSignMessageHash(int base, const std::string& keyShareName, const std::string& messageHash);
virtual Json::Value getPublicECDSAKey(const std::string& keyName); virtual Json::Value getPublicECDSAKey(const std::string& keyName);
...@@ -56,7 +56,7 @@ Json::Value blsSignMessageHashImpl(const std::string& keyShareName, const std::s ...@@ -56,7 +56,7 @@ Json::Value blsSignMessageHashImpl(const std::string& keyShareName, const std::s
Json::Value importECDSAKeyImpl(const std::string& key, const std::string& keyName); Json::Value importECDSAKeyImpl(const std::string& key, const std::string& keyName);
Json::Value generateECDSAKeyImpl(); Json::Value generateECDSAKeyImpl();
Json::Value renameESDSAKeyImpl(const std::string& KeyName, const std::string& tempKeyName); Json::Value renameECDSAKeyImpl(const std::string& KeyName, const std::string& tempKeyName);
Json::Value ecdsaSignMessageHashImpl(int base, const std::string& keyName, const std::string& messageHash); Json::Value ecdsaSignMessageHashImpl(int base, const std::string& keyName, const std::string& messageHash);
Json::Value getPublicECDSAKeyImpl(const std::string& keyName); Json::Value getPublicECDSAKeyImpl(const std::string& keyName);
......
...@@ -17,7 +17,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer> ...@@ -17,7 +17,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
this->bindAndAddMethod(jsonrpc::Procedure("importECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "key",jsonrpc::JSON_STRING,"keyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::importECDSAKeyI); this->bindAndAddMethod(jsonrpc::Procedure("importECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "key",jsonrpc::JSON_STRING,"keyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::importECDSAKeyI);
this->bindAndAddMethod(jsonrpc::Procedure("generateECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, NULL), &AbstractStubServer::generateECDSAKeyI); this->bindAndAddMethod(jsonrpc::Procedure("generateECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, NULL), &AbstractStubServer::generateECDSAKeyI);
this->bindAndAddMethod(jsonrpc::Procedure("renameESDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "KeyName",jsonrpc::JSON_STRING,"tempKeyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::renameESDSAKeyI); this->bindAndAddMethod(jsonrpc::Procedure("renameECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "KeyName",jsonrpc::JSON_STRING,"tempKeyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::renameECDSAKeyI);
this->bindAndAddMethod(jsonrpc::Procedure("getPublicECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "keyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::getPublicECDSAKeyI); this->bindAndAddMethod(jsonrpc::Procedure("getPublicECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "keyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::getPublicECDSAKeyI);
this->bindAndAddMethod(jsonrpc::Procedure("ecdsaSignMessageHash", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "base",jsonrpc::JSON_INTEGER,"keyName",jsonrpc::JSON_STRING,"messageHash",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::ecdsaSignMessageHashI); this->bindAndAddMethod(jsonrpc::Procedure("ecdsaSignMessageHash", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "base",jsonrpc::JSON_INTEGER,"keyName",jsonrpc::JSON_STRING,"messageHash",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::ecdsaSignMessageHashI);
...@@ -47,9 +47,9 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer> ...@@ -47,9 +47,9 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
(void)request; (void)request;
response = this->generateECDSAKey(); response = this->generateECDSAKey();
} }
inline virtual void renameESDSAKeyI(const Json::Value &request, Json::Value &response) inline virtual void renameECDSAKeyI(const Json::Value &request, Json::Value &response)
{ {
response = this->renameESDSAKey(request["KeyName"].asString(), request["tempKeyName"].asString()); response = this->renameECDSAKey(request["KeyName"].asString(), request["tempKeyName"].asString());
} }
inline virtual void getPublicECDSAKeyI(const Json::Value &request, Json::Value &response) inline virtual void getPublicECDSAKeyI(const Json::Value &request, Json::Value &response)
{ {
...@@ -88,7 +88,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer> ...@@ -88,7 +88,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
virtual Json::Value blsSignMessageHash(const std::string& keyShareName, const std::string& messageHash, int n, int signerIndex, int t) = 0; virtual Json::Value blsSignMessageHash(const std::string& keyShareName, const std::string& messageHash, int n, int signerIndex, int t) = 0;
virtual Json::Value importECDSAKey(const std::string& key, const std::string& keyName) = 0; virtual Json::Value importECDSAKey(const std::string& key, const std::string& keyName) = 0;
virtual Json::Value generateECDSAKey() = 0; virtual Json::Value generateECDSAKey() = 0;
virtual Json::Value renameESDSAKey(const std::string& KeyName, const std::string& tempKeyName) = 0; virtual Json::Value renameECDSAKey(const std::string& KeyName, const std::string& tempKeyName) = 0;
virtual Json::Value getPublicECDSAKey(const std::string& keyName) = 0; virtual Json::Value getPublicECDSAKey(const std::string& keyName) = 0;
virtual Json::Value ecdsaSignMessageHash(int base, const std::string& keyName, const std::string& messageHash) = 0; virtual Json::Value ecdsaSignMessageHash(int base, const std::string& keyName, const std::string& messageHash) = 0;
......
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
}, },
{ {
"name": "renameESDSAKey", "name": "renameECDSAKey",
"params": { "params": {
"tempKeyName": "key1", "tempKeyName": "key1",
"KeyName": "key2" "KeyName": "key2"
......
...@@ -61,12 +61,12 @@ class StubClient : public jsonrpc::Client ...@@ -61,12 +61,12 @@ class StubClient : public jsonrpc::Client
else else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
} }
Json::Value renameESDSAKey(const std::string& KeyName, const std::string& tempKeyName) throw (jsonrpc::JsonRpcException) Json::Value renameECDSAKey(const std::string& KeyName, const std::string& tempKeyName) throw (jsonrpc::JsonRpcException)
{ {
Json::Value p; Json::Value p;
p["KeyName"] = KeyName; p["KeyName"] = KeyName;
p["tempKeyName"] = tempKeyName; p["tempKeyName"] = tempKeyName;
Json::Value result = this->CallMethod("renameESDSAKey",p); Json::Value result = this->CallMethod("renameECDSAKey",p);
if (result.isObject()) if (result.isObject())
return result; return result;
else else
......
...@@ -722,6 +722,8 @@ TEST_CASE("API test", "[api_test]") { ...@@ -722,6 +722,8 @@ TEST_CASE("API test", "[api_test]") {
try { try {
//levelDb->deleteOlegKey("0"); //levelDb->deleteOlegKey("0");
//levelDb->deleteOlegKey("1"); //levelDb->deleteOlegKey("1");
levelDb->deleteDHDKGKey("p2_0:");
levelDb->deleteDHDKGKey("p2_1:");
//cout << c.generateECDSAKey() << endl; //cout << c.generateECDSAKey() << endl;
......
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