Unverified Commit 93b8db50 authored by svetaro's avatar svetaro

SKALE-1779 Add key name correctness check

parent d3d3523c
......@@ -10,6 +10,8 @@
#include <gmp.h>
#include <random>
static std::default_random_engine rand_gen((unsigned int) time(0));
std::vector<std::string> gen_ecdsa_key(){
char *errMsg = (char *)calloc(1024, 1);
int err_status = 0;
......@@ -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_len %d " << enc_len << std::endl;
std::default_random_engine rand_gen((unsigned int) time(0));
unsigned long seed = rand_gen();
std::cerr << "seed is " << seed << std::endl;
gmp_randstate_t state;
......
......@@ -29,6 +29,19 @@
#include "SGXWalletServer.h"
#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,
serverVersion_t type)
: AbstractStubServer(connector, type) {}
......@@ -179,7 +192,7 @@ Json::Value generateECDSAKeyImpl() {
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;
result["status"] = 0;
result["errorMessage"] = "";
......@@ -189,14 +202,19 @@ Json::Value renameESDSAKeyImpl(const std::string& KeyName, const std::string& te
std::string prefix = tempKeyName.substr(0,8);
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:") {
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::cerr << "new key name is " << KeyName <<std::endl;
writeDataToDB(KeyName, *key_ptr);
levelDb->deleteTempNEK(tempKeyName);
......@@ -417,7 +435,7 @@ Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::s
//std::cerr << sshares << std::endl;
//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());
if ( res){
......@@ -523,9 +541,9 @@ Json::Value SGXWalletServer::generateECDSAKey() {
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);
return renameESDSAKeyImpl(KeyName, tempKeyName);
return renameECDSAKeyImpl(KeyName, tempKeyName);
}
Json::Value SGXWalletServer::getPublicECDSAKey(const std::string &_keyName) {
......@@ -668,3 +686,4 @@ void writeDataToDB(const string & Name, const string &value) {
levelDb->writeString(key, value);
}
......@@ -25,7 +25,7 @@ public:
virtual Json::Value importECDSAKey(const std::string& key, const std::string& keyName);
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 getPublicECDSAKey(const std::string& keyName);
......@@ -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 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 getPublicECDSAKeyImpl(const std::string& keyName);
......
......@@ -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("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("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>
(void)request;
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)
{
......@@ -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 importECDSAKey(const std::string& key, const std::string& keyName) = 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 ecdsaSignMessageHash(int base, const std::string& keyName, const std::string& messageHash) = 0;
......
......@@ -56,7 +56,7 @@
},
{
"name": "renameESDSAKey",
"name": "renameECDSAKey",
"params": {
"tempKeyName": "key1",
"KeyName": "key2"
......
......@@ -61,12 +61,12 @@ class StubClient : public jsonrpc::Client
else
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;
p["KeyName"] = KeyName;
p["tempKeyName"] = tempKeyName;
Json::Value result = this->CallMethod("renameESDSAKey",p);
Json::Value result = this->CallMethod("renameECDSAKey",p);
if (result.isObject())
return result;
else
......
......@@ -722,6 +722,8 @@ TEST_CASE("API test", "[api_test]") {
try {
//levelDb->deleteOlegKey("0");
//levelDb->deleteOlegKey("1");
levelDb->deleteDHDKGKey("p2_0:");
levelDb->deleteDHDKGKey("p2_1:");
//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