sgxwall.cpp 5.75 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
q  
kladko committed
37 38
#include "ZMQServer.h"

kladko's avatar
kladko committed
39
#include "testw.h"
kladko's avatar
kladko committed
40
#include "sgxwall.h"
kladko's avatar
kladko committed
41 42
#include "sgxwallet.h"

kladko's avatar
q  
kladko committed
43

kladko's avatar
kladko committed
44
void SGXWallet::usage() {
kladko's avatar
kladko committed
45
    cerr << "usage: sgxwallet\n";
46
    exit(-21);
kladko's avatar
kladko committed
47 48
}

kladko's avatar
kladko committed
49
void SGXWallet::printUsage() {
kladko's avatar
kladko committed
50 51 52
    cerr << "\nAvailable flags:\n";
    cerr << "\nDebug flags:\n\n";
    cerr << "   -v  Verbose mode: turn on debug output\n";
53
    cerr << "   -V Detailed verbose mode: turn on debug and trace outputs\n";
kladko's avatar
kladko committed
54
    cerr << "\nBackup, restore, update flags:\n\n";
kladko's avatar
kladko committed
55
    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
56 57 58 59 60
    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
61 62
}

kladko's avatar
kladko committed
63

64
void SGXWallet::serializeKeys(const vector<string>& _ecdsaKeyNames, const vector<string>& _blsKeyNames, const string& _fileName) {
kladko's avatar
kladko committed
65 66 67 68 69 70
    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);
71 72 73 74 75 76

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

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

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

    ofstream fs;

    fs.open(_fileName);

    fs << top;

    fs.close();
kladko's avatar
kladko committed
89 90
}

kladko's avatar
kladko committed
91

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

    int opt;

    if (argc > 1 && strlen(argv[1]) == 1) {
kladko's avatar
kladko committed
105
        SGXWallet::printUsage();
106
        exit(-22);
kladko's avatar
kladko committed
107 108
    }

kladko's avatar
kladko committed
109
    while ((opt = getopt(argc, argv, "cshd0abyvVnT")) != -1) {
kladko's avatar
kladko committed
110 111
        switch (opt) {
            case 'h':
kladko's avatar
kladko committed
112
                SGXWallet::printUsage();
113
                exit(-24);
kladko's avatar
kladko committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
            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
137
                enterBackupKeyOption = false;
kladko's avatar
kladko committed
138 139
                break;
            case 'b':
kladko's avatar
kladko committed
140
                enterBackupKeyOption = true;
kladko's avatar
kladko committed
141 142 143 144
                break;
            case 'y':
                autoconfirmOption = true;
                break;
kladko's avatar
kladko committed
145 146 147
            case 'T':
                generateTestKeys = true;
                break;
kladko's avatar
kladko committed
148
            default:
kladko's avatar
kladko committed
149
                SGXWallet::printUsage();
150
                exit(-23);
kladko's avatar
kladko committed
151 152 153 154
                break;
        }
    }

kladko's avatar
kladko committed
155 156 157 158 159 160 161 162 163 164
    uint64_t logLevel = L_INFO;

    if (printDebugInfoOption) {
        logLevel = L_DEBUG;
    }

    if (printTraceInfoOption) {
        logLevel = L_TRACE;
    }

kladko's avatar
kladko committed
165
    setFullOptions(logLevel, useHTTPSOption, autoconfirmOption, enterBackupKeyOption);
kladko's avatar
kladko committed
166 167 168

    uint32_t enclaveLogLevel = L_INFO;

kladko's avatar
kladko committed
169
    if (printDebugInfoOption) {
kladko's avatar
kladko committed
170 171 172
        enclaveLogLevel = L_DEBUG;
    }

kladko's avatar
kladko committed
173 174 175 176
    if (printTraceInfoOption) {
        enclaveLogLevel = L_TRACE;
    }

kladko's avatar
kladko committed
177
    initAll(enclaveLogLevel, checkClientCertOption, checkClientCertOption, autoSignClientCertOption, generateTestKeys);
kladko's avatar
kladko committed
178

179 180 181
    ifstream is("sgx_data/4node.json");

    if (generateTestKeys && !is.good()) {
kladko's avatar
kladko committed
182 183 184 185 186 187 188 189 190 191 192
        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;

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

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

kladko's avatar
kladko committed
197 198 199
        schainID = 2;
        dkgID = 2;

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

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

kladko's avatar
kladko committed
204
        cerr << "Successfully completed generating test keys into sgx_data" << endl;
kladko's avatar
kladko committed
205 206
    }

kladko's avatar
q  
kladko committed
207 208


kladko's avatar
kladko committed
209 210 211 212 213 214
    while (true) {
        sleep(10);
    }

    return 0;
}