SKALE-4091 fix multithreading issues for generating certs

parent b034e149
...@@ -59,14 +59,16 @@ SGXRegistrationServer::SGXRegistrationServer(AbstractServerConnector &connector, ...@@ -59,14 +59,16 @@ SGXRegistrationServer::SGXRegistrationServer(AbstractServerConnector &connector,
: AbstractRegServer(connector, type), autoSign(_autoSign) {} : AbstractRegServer(connector, type), autoSign(_autoSign) {}
Json::Value signCertificateImpl(const string &_csr, bool _autoSign = false) { Json::Value SGXRegistrationServer::SignCertificate(const string &csr) {
spdlog::info(__FUNCTION__); spdlog::info(__FUNCTION__);
INIT_RESULT(result) INIT_RESULT(result)
result["result"] = false; result["result"] = false;
try { try {
string hash = cryptlite::sha256::hash_hex(_csr); std::lock_guard<std::mutex> lock(m);
string hash = cryptlite::sha256::hash_hex(csr);
if (system("ls " CERT_DIR "/" CERT_CREATE_COMMAND) != 0) { if (system("ls " CERT_DIR "/" CERT_CREATE_COMMAND) != 0) {
spdlog::error("cert/create_client_cert does not exist"); spdlog::error("cert/create_client_cert does not exist");
...@@ -76,7 +78,7 @@ Json::Value signCertificateImpl(const string &_csr, bool _autoSign = false) { ...@@ -76,7 +78,7 @@ Json::Value signCertificateImpl(const string &_csr, bool _autoSign = false) {
string csr_name = string(CERT_DIR) + "/" + hash + ".csr"; string csr_name = string(CERT_DIR) + "/" + hash + ".csr";
ofstream outfile(csr_name); ofstream outfile(csr_name);
outfile.exceptions(std::ifstream::failbit | std::ifstream::badbit); outfile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
outfile << _csr << endl; outfile << csr << endl;
outfile.close(); outfile.close();
if (system(("ls " + csr_name).c_str()) != 0) { if (system(("ls " + csr_name).c_str()) != 0) {
...@@ -85,11 +87,11 @@ Json::Value signCertificateImpl(const string &_csr, bool _autoSign = false) { ...@@ -85,11 +87,11 @@ Json::Value signCertificateImpl(const string &_csr, bool _autoSign = false) {
} }
if (system(("openssl req -in " + csr_name).c_str()) != 0) { if (system(("openssl req -in " + csr_name).c_str()) != 0) {
spdlog::error("Incorrect CSR format: {}", _csr); spdlog::error("Incorrect CSR format: {}", csr);
throw SGXException(FAIL_TO_CREATE_CERTIFICATE, "Incorrect CSR format "); throw SGXException(FAIL_TO_CREATE_CERTIFICATE, "Incorrect CSR format ");
} }
if (_autoSign) { if (autoSign) {
string genCert = string("cd ") + CERT_DIR + "&& ./" string genCert = string("cd ") + CERT_DIR + "&& ./"
+ CERT_CREATE_COMMAND + " " + hash ; + CERT_CREATE_COMMAND + " " + hash ;
...@@ -104,7 +106,7 @@ Json::Value signCertificateImpl(const string &_csr, bool _autoSign = false) { ...@@ -104,7 +106,7 @@ Json::Value signCertificateImpl(const string &_csr, bool _autoSign = false) {
} }
} else { } else {
string db_key = "CSR:HASH:" + hash; string db_key = "CSR:HASH:" + hash;
LevelDB::getCsrStatusDb()->writeDataUnique(db_key, _csr); LevelDB::getCsrStatusDb()->writeDataUnique(db_key, csr);
} }
result["result"] = true; result["result"] = true;
...@@ -115,7 +117,9 @@ Json::Value signCertificateImpl(const string &_csr, bool _autoSign = false) { ...@@ -115,7 +117,9 @@ Json::Value signCertificateImpl(const string &_csr, bool _autoSign = false) {
RETURN_SUCCESS(result) RETURN_SUCCESS(result)
} }
Json::Value getCertificateImpl(const string &hash) { Json::Value SGXRegistrationServer::GetCertificate(const string &hash) {
spdlog::info(__FUNCTION__);
Json::Value result; Json::Value result;
string cert; string cert;
...@@ -151,17 +155,6 @@ Json::Value getCertificateImpl(const string &hash) { ...@@ -151,17 +155,6 @@ Json::Value getCertificateImpl(const string &hash) {
} }
Json::Value SGXRegistrationServer::SignCertificate(const string &csr) {
spdlog::info(__FUNCTION__);
return signCertificateImpl(csr, autoSign);
}
Json::Value SGXRegistrationServer::GetCertificate(const string &hash) {
spdlog::info(__FUNCTION__);
return getCertificateImpl(hash);
}
void SGXRegistrationServer::initRegistrationServer(bool _autoSign) { void SGXRegistrationServer::initRegistrationServer(bool _autoSign) {
httpServer = make_shared<HttpServer>(BASE_PORT + 1); httpServer = make_shared<HttpServer>(BASE_PORT + 1);
server = make_shared<SGXRegistrationServer>(*httpServer, server = make_shared<SGXRegistrationServer>(*httpServer,
...@@ -190,7 +183,6 @@ int SGXRegistrationServer::exitServer() { ...@@ -190,7 +183,6 @@ int SGXRegistrationServer::exitServer() {
return 0; return 0;
} }
shared_ptr<SGXRegistrationServer> SGXRegistrationServer::getServer() { shared_ptr<SGXRegistrationServer> SGXRegistrationServer::getServer() {
CHECK_STATE(server); CHECK_STATE(server);
return server; return server;
......
...@@ -39,23 +39,19 @@ using namespace jsonrpc; ...@@ -39,23 +39,19 @@ using namespace jsonrpc;
using namespace std; using namespace std;
class SGXRegistrationServer : public AbstractRegServer { class SGXRegistrationServer : public AbstractRegServer {
recursive_mutex m; mutex m;
bool autoSign; bool autoSign;
static shared_ptr <HttpServer> httpServer; static shared_ptr <HttpServer> httpServer;
static shared_ptr <SGXRegistrationServer> server; static shared_ptr <SGXRegistrationServer> server;
public: public:
static shared_ptr <SGXRegistrationServer> getServer(); static shared_ptr <SGXRegistrationServer> getServer();
SGXRegistrationServer(AbstractServerConnector &connector, serverVersion_t type, bool _autoSign = false); SGXRegistrationServer(AbstractServerConnector &connector, serverVersion_t type, bool _autoSign = false);
virtual Json::Value SignCertificate(const string &csr); virtual Json::Value SignCertificate(const string &csr);
virtual Json::Value GetCertificate(const string &hash); virtual Json::Value GetCertificate(const string &hash);
......
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