CSRManagerServer.cpp 3.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
//
// Created by kladko on 12/24/19.
//

#include "CSRManagerServer.h"
#include "RPCException.h"
#include "sgxwallet_common.h"

#include <iostream>
#include <fstream>

#include <jsonrpccpp/server/connectors/httpserver.h>

14
#include "spdlog/spdlog.h"
15
#include "common.h"
16

17 18 19 20 21 22 23 24 25

CSRManagerServer *cs = nullptr;
jsonrpc::HttpServer *hs3 = nullptr;


CSRManagerServer::CSRManagerServer(AbstractServerConnector &connector,
    serverVersion_t type):abstractCSRManagerServer(connector, type){}


kladko's avatar
kladko committed
26 27
Json::Value getUnsignedCSRsImpl(){
  spdlog::info("Enter getUnsignedCSRsImpl");
28 29 30 31 32
  Json::Value result;
  result["status"] = 0;
  result["errorMessage"] = "";

  try{
33 34
    vector<string> hashes_vect = LevelDB::getCsrDb()->writeKeysToVector1(MAX_CSR_NUM);
    for (int i = 0; i < (int) hashes_vect.size(); i++){
35 36 37
      result["hashes"][i] = hashes_vect.at(i);
    }
  } catch (RPCException &_e) {
38
    cerr << " err str " << _e.errString << endl;
39 40 41 42 43 44 45 46
    result["status"] = _e.status;
    result["errorMessage"] = _e.errString;

  }

  return result;
}

kladko's avatar
kladko committed
47
Json::Value signByHashImpl(const string& hash, int status){
48 49 50 51 52 53 54 55
  Json::Value result;
  result["errorMessage"] = "";

  try{
    if ( !(status == 0 || status == 2)){
      throw RPCException(-111, "Invalid csr status");
    }

56 57
    string csr_db_key = "CSR:HASH:" + hash;
    shared_ptr<string> csr_ptr = LevelDB::getCsrDb()->readString(csr_db_key);
58 59 60
    if (csr_ptr == nullptr){
      throw RPCException(KEY_SHARE_DOES_NOT_EXIST, "HASH DOES NOT EXIST IN DB");
    }
61 62

    if (status == 0) {
63 64 65
      string csr_name = "sgx_data/cert/" + hash + ".csr";
      ofstream outfile(csr_name);
      outfile << *csr_ptr << endl;
66 67
      outfile.close();
      if (access(csr_name.c_str(), F_OK) != 0) {
68
        LevelDB::getCsrDb()->deleteKey(csr_db_key);
69 70 71
        throw RPCException(FILE_NOT_FOUND, "Csr does not exist");
      }

72
      string signClientCert = "cd sgx_data/cert && ./create_client_cert " + hash;
73 74

      if (system(signClientCert.c_str()) == 0) {
75
        spdlog::info("CLIENT CERTIFICATE IS SUCCESSFULLY GENERATED");
76
      } else {
77
        spdlog::info("CLIENT CERTIFICATE GENERATION FAILED");
78 79 80 81
        LevelDB::getCsrDb()->deleteKey(csr_db_key);
        string status_db_key = "CSR:HASH:" + hash + "STATUS:";
        LevelDB::getCsrStatusDb()->deleteKey(status_db_key);
        LevelDB::getCsrStatusDb()->writeDataUnique(status_db_key, "-1");
82 83 84 85 86
        throw RPCException(FAIL_TO_CREATE_CERTIFICATE, "CLIENT CERTIFICATE GENERATION FAILED");
        //exit(-1);
      }
    }

87 88 89 90
    LevelDB::getCsrDb()->deleteKey(csr_db_key);
    string status_db_key = "CSR:HASH:" + hash + "STATUS:";
    LevelDB::getCsrStatusDb()->deleteKey(status_db_key);
    LevelDB::getCsrStatusDb()->writeDataUnique(status_db_key, to_string(status));
91 92 93 94

    result["status"] = status;

  } catch (RPCException &_e) {
95
    cerr << " err str " << _e.errString << endl;
96 97 98 99 100 101 102 103
    result["status"] = _e.status;
    result["errorMessage"] = _e.errString;
  }

  return result;
}


kladko's avatar
kladko committed
104
Json::Value CSRManagerServer::getUnsignedCSRs(){
105
  lock_guard<recursive_mutex> lock(m);
kladko's avatar
kladko committed
106
  return getUnsignedCSRsImpl();
107 108
}

kladko's avatar
kladko committed
109
Json::Value CSRManagerServer::signByHash(const string& hash, int status){
110
   lock_guard<recursive_mutex> lock(m);
kladko's avatar
kladko committed
111
   return signByHashImpl(hash, status);
112 113 114 115 116 117 118 119
}

int init_csrmanager_server(){
  hs3 = new jsonrpc::HttpServer(BASE_PORT + 2);
  hs3 -> BindLocalhost();
  cs = new CSRManagerServer(*hs3, JSONRPC_SERVER_V2); // server (json-rpc 2.0)

  if (!cs->StartListening()) {
120
    spdlog::info("CSR manager server could not start listening");
121 122 123
    exit(-1);
  }
  else {
124
    spdlog::info("CSR manager server started on port {}", BASE_PORT + 2);
125 126 127
  }
  return 0;
};