sgxwall.cpp 5.68 KB
Newer Older
kladko's avatar
kladko committed
1
/*
2
    Copyright (C) 2019-Present SKALE Labs
kladko's avatar
kladko committed
3

4
    This file is part of sgxwallet.
kladko's avatar
kladko committed
5

6 7 8 9
    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.
kladko's avatar
kladko committed
10

11 12 13 14
    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.
kladko's avatar
kladko committed
15

16 17
    You should have received a copy of the GNU Affero General Public License
    along with sgxwallet.  If not, see <https://www.gnu.org/licenses/>.
kladko's avatar
kladko committed
18

19 20 21
    @file sgxwall.cpp
    @author Stan Kladko
    @date 2020
kladko's avatar
kladko committed
22 23 24 25 26 27 28 29 30
*/

#include <stdbool.h>

#include "BLSCrypto.h"
#include "ServerInit.h"

#include "SEKManager.h"
#include "SGXWalletServer.h"
kladko's avatar
kladko committed
31 32


kladko's avatar
kladko committed
33 34
#include <fstream>

kladko's avatar
kladko committed
35 36
#include "TestUtils.h"

kladko's avatar
kladko committed
37
#include "testw.h"
kladko's avatar
kladko committed
38
#include "sgxwall.h"
kladko's avatar
kladko committed
39 40
#include "sgxwallet.h"

kladko's avatar
kladko committed
41
void SGXWallet::usage() {
kladko's avatar
kladko committed
42
    cerr << "usage: sgxwallet\n";
kladko's avatar
kladko committed
43 44 45
    exit(1);
}

kladko's avatar
kladko committed
46
void SGXWallet::printUsage() {
kladko's avatar
kladko committed
47 48 49 50 51
    cerr << "\nAvailable flags:\n";
    cerr << "\nDebug flags:\n\n";
    cerr << "   -v  Verbose mode: turn on debug output\n";
    cerr << "   -vv Detailed verbose mode: turn on debug and trace outputs\n";
    cerr << "\nBackup, restore, update flags:\n\n";
kladko's avatar
kladko committed
52
    cerr << "   -b  filename Restore from back up or software update. You will need to put backup key into a file in sgx_data dir. \n";
kladko's avatar
kladko committed
53 54 55 56 57
    cerr << "   -y  Do not ask user to acknowledge receipt of the backup key \n";
    cerr << "\nHTTPS flags:\n\n";
    cerr << "   -n  Launch sgxwallet using http. Default is to use https with a selg-signed server cert.  \n";
    cerr << "   -c  Do not verify SSL client certs\n";
    cerr << "   -s  Sign SSL client certs without human confirmation \n";
kladko's avatar
kladko committed
58 59
}

kladko's avatar
kladko committed
60

61
void SGXWallet::serializeKeys(const vector<string>& _ecdsaKeyNames, const vector<string>& _blsKeyNames, const string& _fileName) {
kladko's avatar
kladko committed
62 63 64 65 66 67
    Json::Value top(Json::objectValue);
    Json::Value ecdsaKeysJson(Json::objectValue);
    Json::Value blsKeysJson(Json::objectValue);

    for (uint i = 0; i < _ecdsaKeyNames.size(); i++) {
        auto key = to_string(i + 1);
68 69 70 71 72 73

        string keyFull(3 - key.size(), '0');
        keyFull.append(key);

        ecdsaKeysJson[keyFull] = _ecdsaKeyNames[i];
        blsKeysJson[keyFull] = _blsKeyNames[i];
kladko's avatar
kladko committed
74 75 76 77 78 79 80 81 82 83 84 85
    }

    top["ecdsaKeyNames"] = ecdsaKeysJson;
    top["blsKeyNames"] = blsKeysJson;

    ofstream fs;

    fs.open(_fileName);

    fs << top;

    fs.close();
kladko's avatar
kladko committed
86 87
}

kladko's avatar
kladko committed
88

kladko's avatar
kladko committed
89
int main(int argc, char *argv[]) {
kladko's avatar
kladko committed
90
    bool enterBackupKeyOption  = false;
kladko's avatar
kladko committed
91 92 93 94 95 96
    bool useHTTPSOption = true;
    bool printDebugInfoOption = false;
    bool printTraceInfoOption = false;
    bool autoconfirmOption = false;
    bool checkClientCertOption = true;
    bool autoSignClientCertOption = false;
kladko's avatar
kladko committed
97
    bool generateTestKeys = false;
kladko's avatar
kladko committed
98 99 100 101

    int opt;

    if (argc > 1 && strlen(argv[1]) == 1) {
kladko's avatar
kladko committed
102
        SGXWallet::printUsage();
kladko's avatar
kladko committed
103 104 105
        exit(1);
    }

kladko's avatar
kladko committed
106
    while ((opt = getopt(argc, argv, "cshd0abyvVnT")) != -1) {
kladko's avatar
kladko committed
107 108
        switch (opt) {
            case 'h':
kladko's avatar
kladko committed
109
                SGXWallet::printUsage();
kladko's avatar
kladko committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
                exit(0);
            case 'c':
                checkClientCertOption = false;
                break;
            case 's':
                autoSignClientCertOption = true;
                break;
            case 'd':
                printDebugInfoOption = true;
                break;
            case 'v':
                printDebugInfoOption = true;
                break;
            case 'V':
                printDebugInfoOption = true;
                printTraceInfoOption = true;
                break;
            case '0':
                useHTTPSOption = false;
                break;
            case 'n':
                useHTTPSOption = false;
                break;                
            case 'a':
kladko's avatar
kladko committed
134
                enterBackupKeyOption = false;
kladko's avatar
kladko committed
135 136
                break;
            case 'b':
kladko's avatar
kladko committed
137
                enterBackupKeyOption = true;
kladko's avatar
kladko committed
138 139 140 141
                break;
            case 'y':
                autoconfirmOption = true;
                break;
kladko's avatar
kladko committed
142 143 144
            case 'T':
                generateTestKeys = true;
                break;
kladko's avatar
kladko committed
145
            default:
kladko's avatar
kladko committed
146
                SGXWallet::printUsage();
kladko's avatar
kladko committed
147 148 149 150 151
                exit(1);
                break;
        }
    }

kladko's avatar
kladko committed
152 153 154 155 156 157 158 159 160 161
    uint64_t logLevel = L_INFO;

    if (printDebugInfoOption) {
        logLevel = L_DEBUG;
    }

    if (printTraceInfoOption) {
        logLevel = L_TRACE;
    }

kladko's avatar
kladko committed
162
    setFullOptions(logLevel, useHTTPSOption, autoconfirmOption, enterBackupKeyOption);
kladko's avatar
kladko committed
163 164 165

    uint32_t enclaveLogLevel = L_INFO;

kladko's avatar
kladko committed
166
    if (printDebugInfoOption) {
kladko's avatar
kladko committed
167 168 169
        enclaveLogLevel = L_DEBUG;
    }

kladko's avatar
kladko committed
170 171 172 173
    if (printTraceInfoOption) {
        enclaveLogLevel = L_TRACE;
    }

kladko's avatar
kladko committed
174 175
    initAll(enclaveLogLevel, checkClientCertOption, autoSignClientCertOption);

176 177 178
    ifstream is("sgx_data/4node.json");

    if (generateTestKeys && !is.good()) {
kladko's avatar
kladko committed
179 180 181 182 183 184 185 186 187 188 189
        cerr << "Generating test keys ..." << endl;

        HttpClient client(RPC_ENDPOINT);
        StubClient c(client, JSONRPC_CLIENT_V2);

        vector<string> ecdsaKeyNames;
        vector<string> blsKeyNames;

        int schainID = 1;
        int dkgID = 1;

190
        TestUtils::doDKG(c, 4, 3, ecdsaKeyNames, blsKeyNames, schainID, dkgID);
kladko's avatar
kladko committed
191

kladko's avatar
kladko committed
192 193
        SGXWallet::serializeKeys(ecdsaKeyNames, blsKeyNames, "sgx_data/4node.json");

kladko's avatar
kladko committed
194 195 196
        schainID = 2;
        dkgID = 2;

197
        TestUtils::doDKG(c, 16, 11, ecdsaKeyNames, blsKeyNames, schainID, dkgID);
kladko's avatar
kladko committed
198

kladko's avatar
kladko committed
199 200
        SGXWallet::serializeKeys(ecdsaKeyNames, blsKeyNames, "sgx_data/16node.json");

kladko's avatar
kladko committed
201
        cerr << "Successfully completed generating test keys into sgx_data" << endl;
kladko's avatar
kladko committed
202 203
    }

kladko's avatar
kladko committed
204 205 206 207 208 209
    while (true) {
        sleep(10);
    }

    return 0;
}