CSRManagerServer.cpp 4.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
    Copyright (C) 2019-Present SKALE Labs

    This file is part of sgxwallet.

    sgxwallet is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    sgxwallet is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with sgxwallet.  If not, see <https://www.gnu.org/licenses/>.

    @file CSRManager.cpp
    @author Stan Kladko
    @date 2019
*/

24 25 26 27

#include <iostream>
#include <fstream>

kladko's avatar
kladko committed
28 29 30



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

kladko's avatar
kladko committed
33 34 35 36 37 38 39

#include "CSRManagerServer.h"
#include "SGXException.h"
#include "sgxwallet_common.h"


#include "Log.h"
40
#include "common.h"
41

42

kladko's avatar
kladko committed
43 44
shared_ptr<CSRManagerServer> CSRManagerServer::cs = nullptr;
shared_ptr<jsonrpc::HttpServer> CSRManagerServer::hs3 = nullptr;
45 46 47


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


51
Json::Value getUnsignedCSRsImpl() {
kladko's avatar
kladko committed
52 53
    spdlog::info(__FUNCTION__);
    INIT_RESULT(result)
54

55 56 57 58 59
    try {
        vector<string> hashes_vect = LevelDB::getCsrDb()->writeKeysToVector1(MAX_CSR_NUM);
        for (int i = 0; i < (int) hashes_vect.size(); i++) {
            result["hashes"][i] = hashes_vect.at(i);
        }
kladko's avatar
kladko committed
60
    } HANDLE_SGX_EXCEPTION(result);
61

62 63
    return result;
}
64

65 66 67 68 69 70 71 72 73 74 75 76 77 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
Json::Value signByHashImpl(const string &hash, int status) {
    Json::Value result;
    result["errorMessage"] = "";

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

        string csr_db_key = "CSR:HASH:" + hash;
        shared_ptr<string> csr_ptr = LevelDB::getCsrDb()->readString(csr_db_key);
        if (csr_ptr == nullptr) {
            throw SGXException(KEY_SHARE_DOES_NOT_EXIST, "HASH DOES NOT EXIST IN DB");
        }

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

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

            if (system(signClientCert.c_str()) == 0) {
                spdlog::info("CLIENT CERTIFICATE IS SUCCESSFULLY GENERATED");
            } else {
                spdlog::info("CLIENT CERTIFICATE GENERATION FAILED");
                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");
                throw SGXException(FAIL_TO_CREATE_CERTIFICATE, "CLIENT CERTIFICATE GENERATION FAILED");
                //exit(-1);
            }
        }
104

105 106 107
        LevelDB::getCsrDb()->deleteKey(csr_db_key);
        string status_db_key = "CSR:HASH:" + hash + "STATUS:";
        LevelDB::getCsrStatusDb()->deleteKey(status_db_key);
108
        LevelDB::getCsrStatusDb()->writeDataUnique(status_db_key, to_string(status));
109

110
        result["status"] = status;
111

kladko's avatar
kladko committed
112
    } HANDLE_SGX_EXCEPTION(result)
113

114
    return result;
115 116 117
}


118
Json::Value CSRManagerServer::getUnsignedCSRs() {
kladko's avatar
kladko committed
119
    LOCK(m)
120
    return getUnsignedCSRsImpl();
121 122
}

123
Json::Value CSRManagerServer::signByHash(const string &hash, int status) {
kladko's avatar
kladko committed
124
    LOCK(m)
125
    return signByHashImpl(hash, status);
126 127
}

kladko's avatar
kladko committed
128
int CSRManagerServer::initCSRManagerServer() {
kladko's avatar
kladko committed
129
    hs3 = make_shared<jsonrpc::HttpServer>(BASE_PORT + 2);
130
    hs3->BindLocalhost();
kladko's avatar
kladko committed
131
    cs = make_shared<CSRManagerServer>(*hs3, JSONRPC_SERVER_V2); // server (json-rpc 2.0)
132 133 134 135 136 137 138 139

    if (!cs->StartListening()) {
        spdlog::info("CSR manager server could not start listening");
        exit(-1);
    } else {
        spdlog::info("CSR manager server started on port {}", BASE_PORT + 2);
    }
    return 0;
140
};