CSRManagerServer.cpp 3.54 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 15
#include "spdlog/spdlog.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){}


Json::Value GetUnsignedCSRsImpl(){
26
  spdlog::info("Enter GetUnsignedCSRsImpl");
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  Json::Value result;
  result["status"] = 0;
  result["errorMessage"] = "";
  //result["hashes"] =;

  try{
    std::vector<std::string> hashes_vect = csrDb->writeKeysToVector1(MAX_CSR_NUM);
    for (int i = 0; i < hashes_vect.size(); i++){
      result["hashes"][i] = hashes_vect.at(i);
    }
  } catch (RPCException &_e) {
    std::cerr << " err str " << _e.errString << std::endl;
    result["status"] = _e.status;
    result["errorMessage"] = _e.errString;

  }

  return result;
}

Json::Value SignByHashImpl(const std::string& hash, int status){
  Json::Value result;
  result["errorMessage"] = "";

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

    std::string csr_db_key = "CSR:HASH:" + hash;
    std::shared_ptr<std::string> csr_ptr = csrDb->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
      std::string csr_name = "sgx_data/cert/" + hash + ".csr";
64 65 66 67
      std::ofstream outfile(csr_name);
      outfile << *csr_ptr << std::endl;
      outfile.close();
      if (access(csr_name.c_str(), F_OK) != 0) {
68
        csrDb->deleteKey(csr_db_key);
69 70 71
        throw RPCException(FILE_NOT_FOUND, "Csr does not exist");
      }

72
      std::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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        csrDb->deleteKey(csr_db_key);
        std::string status_db_key = "CSR:HASH:" + hash + "STATUS:";
        csrStatusDb->deleteKey(status_db_key);
        csrStatusDb->writeDataUnique(status_db_key, "-1");
        throw RPCException(FAIL_TO_CREATE_CERTIFICATE, "CLIENT CERTIFICATE GENERATION FAILED");
        //exit(-1);
      }
    }

    csrDb->deleteKey(csr_db_key);
    std::string status_db_key = "CSR:HASH:" + hash + "STATUS:";
    csrStatusDb->deleteKey(status_db_key);
    csrStatusDb->writeDataUnique(status_db_key, std::to_string(status));

    result["status"] = status;

  } catch (RPCException &_e) {
    std::cerr << " err str " << _e.errString << std::endl;
    result["status"] = _e.status;
    result["errorMessage"] = _e.errString;
  }

  return result;
}


Json::Value CSRManagerServer::GetUnsignedCSRs(){
  std::lock_guard<std::recursive_mutex> lock(m);
  return GetUnsignedCSRsImpl();
}

Json::Value CSRManagerServer::SignByHash(const std::string& hash, int status){
   std::lock_guard<std::recursive_mutex> lock(m);
   return SignByHashImpl(hash, status);
}

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;
};