ServerInit.cpp 3.3 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 ServerInit.cpp
    @author Stan Kladko
    @date 2019
*/

kladkogex's avatar
kladkogex committed
24
#include <memory>
25
#include <iostream>
kladkogex's avatar
kladkogex committed
26

27 28 29 30 31
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
kladkogex's avatar
kladkogex committed
32 33 34 35 36

#include "libff/algebra/curves/alt_bn128/alt_bn128_init.hpp"
#include "bls.h"
#include "leveldb/db.h"
#include <jsonrpccpp/server/connectors/httpserver.h>
37 38 39 40

#include "spdlog/spdlog.h"
#include <gmp.h>
#include <sgx_urts.h>
kladkogex's avatar
kladkogex committed
41 42


43
#include "BLSPrivateKeyShareSGX.h"
kladkogex's avatar
kladkogex committed
44 45 46 47 48
#include "sgxwallet_common.h"
#include "create_enclave.h"
#include "secure_enclave_u.h"
#include "sgx_detect.h"
#include "sgxwallet.h"
kladkogex's avatar
kladkogex committed
49
#include "LevelDB.h"
kladkogex's avatar
kladkogex committed
50
#include "SGXWalletServer.h"
51
#include "SGXRegistrationServer.h"
52
#include "SEKManager.h"
53
#include "CSRManagerServer.h"
kladkogex's avatar
kladkogex committed
54 55
#include "BLSCrypto.h"
#include "ServerInit.h"
kladko's avatar
kladko committed
56
#include "SGXWalletServer.hpp"
kladko's avatar
kladko committed
57

58
void initUserSpace() {
kladkogex's avatar
kladkogex committed
59
    libff::init_alt_bn128_params();
kladko's avatar
kladko committed
60
    LevelDB::initDataFolderAndDBs();
kladkogex's avatar
kladkogex committed
61
}
kladkogex's avatar
kladkogex committed
62

63
void initEnclave(uint32_t _logLevel) {
kladkogex's avatar
kladkogex committed
64 65 66 67
    eid = 0;
    updated = 0;

#ifndef SGX_HW_SIM
68
    unsigned long support;
kladkogex's avatar
kladkogex committed
69 70 71 72 73 74 75
    support = get_sgx_support();
    if (!SGX_OK(support)) {
        sgx_support_perror(support);
        exit(1);
    }
#endif

76
    spdlog::debug("SGX_DEBUG_FLAG = {}", SGX_DEBUG_FLAG);
77

kladkogex's avatar
kladkogex committed
78 79 80 81 82
    status = sgx_create_enclave_search(ENCLAVE_NAME, SGX_DEBUG_FLAG, &token,
                                       &updated, &eid, 0);

    if (status != SGX_SUCCESS) {
        if (status == SGX_ERROR_ENCLAVE_FILE_ACCESS) {
83 84
            spdlog::error("sgx_create_enclave: {}: file not found", ENCLAVE_NAME);
            spdlog::error("Did you forget to set LD_LIBRARY_PATH?");
kladkogex's avatar
kladkogex committed
85
        } else {
86
            spdlog::error("sgx_create_enclave_search failed {} {}", ENCLAVE_NAME, status);
kladkogex's avatar
kladkogex committed
87 88 89 90
        }
        exit(1);
    }

91
    spdlog::info("Enclave created and started successfully");
kladkogex's avatar
kladkogex committed
92

93
    status = trustedEnclaveInit(eid, _logLevel);
kladkogex's avatar
kladkogex committed
94
    if (status != SGX_SUCCESS) {
95
        spdlog::error("trustedEnclaveInit failed: {}", status);
kladkogex's avatar
kladkogex committed
96 97 98
        exit(1);
    }

99
    spdlog::info("Enclave libtgmp library and logging initialized successfully");
kladkogex's avatar
kladkogex committed
100
}
kladkogex's avatar
kladkogex committed
101

102
void initAll(uint32_t  _logLevel, bool _checkCert, bool _autoSign) {
kladko's avatar
kladko committed
103 104
    static int sgxServerInited;

kladko's avatar
kladko committed
105
    cout << "Running sgxwallet version:" << SGXWalletServer::getVersion() << endl;
kladko's avatar
kladko committed
106

107
    CHECK_STATE(sgxServerInited == 0)
kladkogex's avatar
kladkogex committed
108
    sgxServerInited = 1;
109
    initEnclave(_logLevel);
110
    initUserSpace();
kladko's avatar
kladko committed
111
    initSEK();
kladkogex's avatar
kladkogex committed
112

kladko's avatar
kladko committed
113
    if (useHTTPS) {
kladko's avatar
kladko committed
114
        SGXWalletServer::initHttpsServer(_checkCert);
kladko's avatar
kladko committed
115
        SGXRegistrationServer::initRegistrationServer(_autoSign);
kladko's avatar
kladko committed
116
        CSRManagerServer::initCSRManagerServer();
117
    } else {
kladko's avatar
kladko committed
118
        SGXWalletServer::initHttpServer();
119
    }
kladkogex's avatar
kladkogex committed
120
}