-
Notifications
You must be signed in to change notification settings - Fork 13
/
KeyManager.cpp
220 lines (198 loc) · 6.49 KB
/
KeyManager.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
/*
* @CopyRight:
* FISCO-BCOS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FISCO-BCOS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FISCO-BCOS. If not, see <http://www.gnu.org/licenses/>
* (c) 2016-2018 fisco-dev contributors.
*/
/**
* @brief : key-manager server
* @author: jimmyshi
* @date: 2018-12-04
*/
#include "KeyManager.h"
#include "libutils/gm/GmCrypto.h"
#include "libutils/origin/OriginCrypto.h"
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <exception>
#include <iostream>
using namespace std;
using namespace dev;
using namespace jsonrpc;
// method
void BaseServer::decDataKey(const Json::Value& request, Json::Value& response)
{
KCLOG(TRACE) << LOG_BADGE("Dec") << LOG_DESC("Receive") << endl
<< request.toStyledString() << endl;
Json::Value res;
try
{
res["dataKey"] = decryptDataKeyHex(request[0u].asString());
res["error"] = 0;
res["info"] = "success";
}
catch (std::exception& e)
{
res["dataKey"] = "";
res["error"] = 1;
res["info"] = string(e.what());
}
response = res;
KCLOG(TRACE) << LOG_BADGE("Dec") << LOG_DESC("Respond") << endl
<< response.toStyledString() << endl;
}
void BaseServer::encDataKey(const Json::Value& request, Json::Value& response)
{
KCLOG(TRACE) << LOG_BADGE("Enc") << LOG_DESC("Receive") << endl
<< request.toStyledString() << endl;
Json::Value res;
try
{
res["dataKey"] = encryptDataKey(request[0u].asString());
res["error"] = 0;
res["info"] = "success";
}
catch (std::exception& e)
{
res["dataKey"] = "";
res["error"] = 1;
res["info"] = string(e.what());
}
response = res;
KCLOG(TRACE) << LOG_BADGE("Enc") << LOG_DESC("Respond") << endl
<< response.toStyledString() << endl;
}
void BaseServer::encWithCipherKey(const Json::Value& request, Json::Value& response)
{
KCLOG(TRACE) << LOG_BADGE("EncWithKey") << LOG_DESC("Receive") << endl
<< request.toStyledString() << endl;
Json::Value res;
try
{
res["dataKey"] = encryptWithCipherKey(request[0u].asString(), request[1u].asString());
res["error"] = 0;
res["info"] = "success";
}
catch (std::exception& e)
{
res["dataKey"] = "";
res["error"] = 1;
res["info"] = string(e.what());
}
response = res;
KCLOG(TRACE) << LOG_BADGE("EncWithKey") << LOG_DESC("Respond") << endl
<< response.toStyledString() << endl;
}
static bool should_exit = false;
static void exit_handler(int sig)
{
should_exit = true;
}
std::string KeyManager::decryptDataKeyHex(const std::string& _cipherDataKey)
{
bytes enData = fromHex(_cipherDataKey);
bytes deData = m_crypto->aesCBCDecrypt(ref(enData), ref(m_superKey));
return toHex(deData);
}
std::string KeyManager::encryptDataKey(const std::string& _dataKey)
{
bytes dataKeyBtyes =
bytesConstRef{(unsigned char*)_dataKey.c_str(), _dataKey.length()}.toBytes();
bytes deData = m_crypto->aesCBCEncrypt(ref(dataKeyBtyes), ref(m_superKey));
return toHex(deData);
}
std::string KeyManager::encryptWithCipherKey(
const std::string& _data, const std::string& _cipherDataKey)
{
bytes cipherDataKeyBytes = fromHex(_cipherDataKey);
bytes readableDataKeyBytes = m_crypto->aesCBCDecrypt(ref(cipherDataKeyBytes), ref(m_superKey));
bytes realDataKey = m_crypto->uniformKey(ref(readableDataKeyBytes));
bytes dataBytes = bytesConstRef{(unsigned char*)_data.c_str(), _data.length()}.toBytes();
bytes encData = m_crypto->aesCBCEncrypt(ref(dataBytes), ref(realDataKey));
return toHex(encData);
}
int main(int argc, char* argv[])
{
if (argc != 3 && argc != 4)
{
cout << "Usage: ./key-manager <port> <superkey> <gm version: opt>" << endl;
cout << "Eg: ./key-manager 8150 123xyz" << endl;
cout << "Eg: ./key-manager 8150 123xyz -g create gm version" << endl;
return 0;
}
dev::Crypto::Ptr crypto;
if (3 == argc)
crypto = std::make_shared<dev::OriginCrypto>();
if (4 == argc)
{
if (0 != strcmp("-g", argv[3]))
{
std::cerr
<< "You gave four paramaters, but the fourth parameter is not recognized, exit"
<< std::endl;
exit(0);
}
crypto = std::make_shared<dev::GmCrypto>();
}
// Parse config
int port;
bytes superKey;
try
{
port = atoi(argv[1]);
string superKeyStr = argv[2];
if (superKeyStr.empty())
{
KCLOG(ERROR) << LOG_BADGE("Load") << LOG_DESC("Superkey is empty") << endl;
throw;
}
// uniform/compress key to a fixed size bytes of size 32/128
superKey = crypto->uniformKey(bytesConstRef(superKeyStr));
}
catch (std::exception& e)
{
KCLOG(ERROR) << LOG_BADGE("Load") << LOG_DESC("Configure params error")
<< LOG_KV("reason", e.what()) << endl;
return 0;
}
// Start key-manager
try
{
HttpServer connector(port);
KeyManager keyManager(connector, superKey, crypto);
if (keyManager.StartListening())
{
// register exit_handler signal
signal(SIGABRT, &exit_handler);
signal(SIGTERM, &exit_handler);
signal(SIGINT, &exit_handler);
KCLOG(TRACE) << LOG_BADGE("Load") << LOG_DESC("key-manager started")
<< LOG_KV("port", port) << endl;
while (!should_exit)
sleep(1);
}
else
{
KCLOG(ERROR) << LOG_BADGE("Load") << LOG_DESC("Start Server failed") << endl;
}
}
catch (jsonrpc::JsonRpcException& e)
{
KCLOG(ERROR) << LOG_BADGE("Load") << LOG_DESC("Start Server exception")
<< LOG_KV("reason", e.what()) << endl;
}
return 0;
}
// curl -X POST --data '{"jsonrpc":"2.0","method":"getDataKey","params":["name"],"id":83}'
// http://127.0.0.1:8150 |jq