-
Notifications
You must be signed in to change notification settings - Fork 35
/
SGXRegistrationServer.cpp
196 lines (152 loc) · 5.83 KB
/
SGXRegistrationServer.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
/*
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 SGXRegistrationServer.cpp
@author Stan Kladko
@date 2019
*/
#include <fstream>
#include <iostream>
#include <sstream>
#include <jsonrpccpp/server/connectors/httpserver.h>
#include <third_party/cryptlite/sha256.h>
#include <stdio.h>
#include "sgxwallet_common.h"
#include "LevelDB.h"
#include "SGXException.h"
#include <thread>
#include <time.h>
#include <functional>
#include "LevelDB.h"
#include "SGXRegistrationServer.h"
#include "Log.h"
#include "common.h"
bool printDebugInfo = false;
bool useHTTPS = false;
bool enterBackupKey = false;
bool autoconfirm = false;
shared_ptr<SGXRegistrationServer> SGXRegistrationServer::server = nullptr;
shared_ptr<HttpServer> SGXRegistrationServer::httpServer = nullptr;
SGXRegistrationServer::SGXRegistrationServer(AbstractServerConnector &connector,
serverVersion_t type,
bool _autoSign)
: AbstractRegServer(connector, type), autoSign(_autoSign) {}
Json::Value SGXRegistrationServer::SignCertificate(const string &csr) {
spdlog::info(__FUNCTION__);
INIT_RESULT(result)
result["result"] = false;
try {
std::lock_guard<std::mutex> lock(m);
string hash = cryptlite::sha256::hash_hex(csr);
if (system("ls " CERT_DIR "/" CERT_CREATE_COMMAND) != 0) {
spdlog::error("cert/create_client_cert does not exist");
throw SGXException(FAIL_TO_CREATE_CERTIFICATE,
"CLIENT CERTIFICATE GENERATION FAILED");
}
string csr_name = string(CERT_DIR) + "/" + hash + ".csr";
ofstream outfile(csr_name);
outfile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
outfile << csr << endl;
outfile.close();
if (system(("ls " + csr_name).c_str()) != 0) {
spdlog::error("could not create csr file");
throw SGXException(FAIL_TO_CREATE_CERTIFICATE,
"CLIENT CERTIFICATE GENERATION FAILED");
}
if (system(("openssl req -in " + csr_name).c_str()) != 0) {
spdlog::error("Incorrect CSR format: {}", csr);
throw SGXException(FAIL_TO_CREATE_CERTIFICATE, "Incorrect CSR format ");
}
if (autoSign) {
string genCert =
string("cd ") + CERT_DIR + "&& ./" + CERT_CREATE_COMMAND + " " + hash;
if (system(genCert.c_str()) == 0) {
spdlog::info("Client cert " + hash + " generated");
} else {
spdlog::error("Client cert generation failed: {} ", genCert);
throw SGXException(FAIL_TO_CREATE_CERTIFICATE,
"CLIENT CERTIFICATE GENERATION FAILED");
}
} else {
string db_key = "CSR:HASH:" + hash;
LevelDB::getCsrStatusDb()->writeDataUnique(db_key, csr);
}
string db_key = "CSR:HASH:" + hash + "STATUS:";
string status = "0";
LevelDB::getCsrStatusDb()->writeDataUnique(db_key, status);
result["result"] = true;
result["hash"] = hash;
}
HANDLE_SGX_EXCEPTION(result)
RETURN_SUCCESS(result)
}
Json::Value SGXRegistrationServer::GetCertificate(const string &hash) {
spdlog::info(__FUNCTION__);
Json::Value result;
string cert;
try {
string db_key = "CSR:HASH:" + hash + "STATUS:";
shared_ptr<string> status_str_ptr =
LevelDB::getCsrStatusDb()->readString(db_key);
if (status_str_ptr == nullptr) {
throw SGXException(CERT_REQUEST_DOES_NOT_EXIST,
"Data with this name does not exist in csr db");
}
int status = atoi(status_str_ptr->c_str());
if (status == 0) {
string crtPath = "cert/" + hash + ".crt";
if (system(("ls " + crtPath).c_str()) != 0) {
throw SGXException(FILE_NOT_FOUND, "Certificate does not exist");
}
ifstream infile(crtPath);
infile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
ostringstream ss;
ss << infile.rdbuf();
infile.close();
cert = ss.str();
}
result["status"] = status;
result["cert"] = cert;
}
HANDLE_SGX_EXCEPTION(result)
RETURN_SUCCESS(result)
}
void SGXRegistrationServer::initRegistrationServer(bool _autoSign) {
httpServer = make_shared<HttpServer>(BASE_PORT + 1);
server = make_shared<SGXRegistrationServer>(
*httpServer, JSONRPC_SERVER_V2,
_autoSign); // hybrid server (json-rpc 1.0 & 2.0)
spdlog::info("Starting registration server on port {} ...", BASE_PORT + 1);
if (!server->StartListening()) {
spdlog::error("Registration server could not start listening on port {}",
BASE_PORT + 1);
throw SGXException(REGISTRATION_SERVER_FAILED_TO_START,
"Registration server could not start listening.");
} else {
spdlog::info("Registration server started on port {}", BASE_PORT + 1);
}
}
int SGXRegistrationServer::exitServer() {
spdlog::info("Stoping registration server");
if (server && !server->StopListening()) {
spdlog::error("Registration server could not be stopped. Will forcefully "
"terminate the app");
} else {
spdlog::info("Registration server stopped");
}
return 0;
}
shared_ptr<SGXRegistrationServer> SGXRegistrationServer::getServer() {
CHECK_STATE(server);
return server;
}