-
Notifications
You must be signed in to change notification settings - Fork 34
/
sgxwall.cpp
244 lines (191 loc) · 6.29 KB
/
sgxwall.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
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 sgxwall.cpp
@author Stan Kladko
@date 2020
*/
#include <csignal>
#include <stdbool.h>
#include "ExitHandler.h"
#include "BLSCrypto.h"
#include "ServerInit.h"
#include "SEKManager.h"
#include "SGXWalletServer.h"
#include <fstream>
#include "TestUtils.h"
#include "zmq_src/ZMQServer.h"
#include "sgxwall.h"
#include "sgxwallet.h"
#include "testw.h"
void SGXWallet::printUsage() {
cerr << "\nAvailable flags:\n";
cerr << "\nDebug flags:\n\n";
cerr << " -v Verbose mode: turn on debug output\n";
cerr << " -V Detailed verbose mode: turn on debug and trace outputs\n";
cerr << "\nBackup, restore, update flags:\n\n";
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";
cerr << " -y Do not ask user to acknowledge receipt of the backup key \n";
cerr << "\nSecurity flags flags:\n\n";
cerr << " -n Use http instead of https. Default is to use https with a "
"selg-signed server cert. Insecure! \n";
cerr
<< " -c Disable client authentication using certificates. Insecure!\n";
cerr << " -s Sign client certificates without human confirmation. "
"Insecure! \n";
cerr << " -e Only owner of the key can access it.\n";
}
void SGXWallet::serializeKeys(const vector<string> &_ecdsaKeyNames,
const vector<string> &_blsKeyNames,
const string &_fileName) {
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);
string keyFull(3 - key.size(), '0');
keyFull.append(key);
ecdsaKeysJson[keyFull] = _ecdsaKeyNames[i];
blsKeysJson[keyFull] = _blsKeyNames[i];
}
top["ecdsaKeyNames"] = ecdsaKeysJson;
top["blsKeyNames"] = blsKeysJson;
ofstream fs;
fs.open(_fileName);
fs << top;
fs.close();
}
void SGXWallet::signalHandler(int signalNo) {
spdlog::info("Received exit signal {}.", signalNo);
ExitHandler::exitHandler(signalNo);
}
int main(int argc, char *argv[]) {
bool enterBackupKeyOption = false;
bool useHTTPSOption = true;
bool printDebugInfoOption = false;
bool printTraceInfoOption = false;
bool autoconfirmOption = false;
bool checkClientCertOption = true;
bool autoSignClientCertOption = false;
bool generateTestKeys = false;
bool checkKeyOwnership = false;
std::signal(SIGABRT, SGXWallet::signalHandler);
int opt;
if (argc > 1 && strlen(argv[1]) == 1) {
SGXWallet::printUsage();
exit(-21);
}
while ((opt = getopt(argc, argv, "cshd0abyvVneT")) != -1) {
switch (opt) {
case 'h':
SGXWallet::printUsage();
exit(-22);
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;
checkKeyOwnership = false;
break;
case 'e':
checkKeyOwnership = true;
break;
case 'a':
enterBackupKeyOption = false;
break;
case 'b':
enterBackupKeyOption = true;
break;
case 'y':
autoconfirmOption = true;
break;
case 'T':
generateTestKeys = true;
break;
default:
SGXWallet::printUsage();
exit(-23);
break;
}
}
uint64_t logLevel = L_INFO;
if (printDebugInfoOption) {
logLevel = L_DEBUG;
}
if (printTraceInfoOption) {
logLevel = L_TRACE;
}
setFullOptions(logLevel, useHTTPSOption, autoconfirmOption,
enterBackupKeyOption);
uint32_t enclaveLogLevel = L_INFO;
if (printDebugInfoOption) {
enclaveLogLevel = L_DEBUG;
}
if (printTraceInfoOption) {
enclaveLogLevel = L_TRACE;
}
cerr << "Calling initAll ..." << endl;
initAll(enclaveLogLevel, checkClientCertOption, checkClientCertOption,
autoSignClientCertOption, generateTestKeys, checkKeyOwnership);
cerr << "Completed initAll." << endl;
// check if test keys already exist
string TEST_KEYS_4_NODE = "sgx_data/4node.json";
ifstream is(TEST_KEYS_4_NODE);
auto keysExist = is.good();
if (keysExist) {
cerr << "Found test keys." << endl;
}
if (generateTestKeys && !keysExist && !ExitHandler::shouldExit()) {
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;
TestUtils::doDKG(c, 4, 3, ecdsaKeyNames, blsKeyNames, schainID, dkgID);
SGXWallet::serializeKeys(ecdsaKeyNames, blsKeyNames, "sgx_data/4node.json");
schainID = 2;
dkgID = 2;
TestUtils::doDKG(c, 16, 11, ecdsaKeyNames, blsKeyNames, schainID, dkgID);
SGXWallet::serializeKeys(ecdsaKeyNames, blsKeyNames,
"sgx_data/16node.json");
cerr << "Successfully completed generating test keys into sgx_data" << endl;
}
while (!ExitHandler::shouldExit()) {
sleep(10);
}
ExitHandler::exit_code_t exitCode = ExitHandler::requestedExitCode();
int signal = ExitHandler::getSignal();
spdlog::info("Will exit with exit code {}", exitCode);
exitAll();
spdlog::info("Exiting with exit code {} and signal", exitCode, signal);
return exitCode;
}