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


64
void initEnclave(uint32_t _logLevel) {
kladkogex's avatar
kladkogex committed
65 66 67 68

    eid = 0;
    updated = 0;

69

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

79
        spdlog::debug("SGX_DEBUG_FLAG = {}", SGX_DEBUG_FLAG);
80

81

kladkogex's avatar
kladkogex committed
82 83 84 85 86
    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) {
87 88
            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
89
        } else {
90
            spdlog::error("sgx_create_enclave_search failed {} {}", ENCLAVE_NAME, status);
kladkogex's avatar
kladkogex committed
91 92 93 94
        }
        exit(1);
    }

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

97
    status = trustedEnclaveInit(eid, _logLevel);
kladkogex's avatar
kladkogex committed
98
    if (status != SGX_SUCCESS) {
99
        spdlog::error("trustedEnclaveInit failed: {}", status);
kladkogex's avatar
kladkogex committed
100 101 102
        exit(1);
    }

103
    spdlog::info("Enclave libtgmp library and logging initialized successfully");
kladko's avatar
kladko committed
104

kladkogex's avatar
kladkogex committed
105
}
kladkogex's avatar
kladkogex committed
106 107


kladko's avatar
kladko committed
108

kladkogex's avatar
kladkogex committed
109

110
void initAll(uint32_t  _logLevel, bool _checkCert, bool _autoSign) {
kladko's avatar
kladko committed
111

kladko's avatar
kladko committed
112 113
    static int sgxServerInited;

kladko's avatar
kladko committed
114
    cout << "Running sgxwallet version:" << SGXWALLET_VERSION << endl;
kladko's avatar
kladko committed
115

116
    CHECK_STATE(sgxServerInited == 0)
kladkogex's avatar
kladkogex committed
117
    sgxServerInited = 1;
118
    initEnclave(_logLevel);
119
    initUserSpace();
kladko's avatar
kladko committed
120
    initSEK();
kladkogex's avatar
kladkogex committed
121

kladko's avatar
kladko committed
122
    if (useHTTPS) {
kladko's avatar
kladko committed
123
        SGXWalletServer::initHttpsServer(_checkCert);
kladko's avatar
kladko committed
124
        SGXRegistrationServer::initRegistrationServer(_autoSign);
kladko's avatar
kladko committed
125
        CSRManagerServer::initCSRManagerServer();
126
    } else {
kladko's avatar
kladko committed
127
        SGXWalletServer::initHttpServer();
128
    }
kladkogex's avatar
kladkogex committed
129
}