-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cc
372 lines (316 loc) · 12 KB
/
client.cc
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <cstdint>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include "absl/strings/string_view.h"
#include "encryption.h"
#include "message.pb.h"
#include "utils.h"
namespace {
// All the handlers for the client-requested operations are defined in this
// unnamed namespace. All the handlers return a WaitForServerResponse value
// indicating if we need to wait for some response from the server or if we can
// continue to the next operations from the user side.
enum class WaitForServerResponse {
kEnabled,
kDisabled
};
WaitForServerResponse HandleGenerateRsaKeysOperation(
std::string* rsa_public_key, std::string* rsa_private_key) {
if (!rsa_public_key->empty() && !rsa_private_key->empty()) {
std::cout << "RSA keys already exist." << std::endl;
return WaitForServerResponse::kDisabled;
}
auto key_pair = GenerateRsaKeyPair();
if (!key_pair.ok()) {
std::cout << key_pair.status() << std::endl;
} else {
*rsa_public_key = std::move(key_pair->public_key);
*rsa_private_key = std::move(key_pair->private_key);
}
return WaitForServerResponse::kDisabled;
}
WaitForServerResponse HandleLoadRsaKeysOperation(
std::string* rsa_public_key, std::string* rsa_private_key) {
if (!rsa_public_key->empty() && !rsa_private_key->empty()) {
std::cout << "RSA keys already exist." << std::endl;
return WaitForServerResponse::kDisabled;
}
auto public_key_filename = RequestStringInput(
"Enter the public key filename");
auto private_key_filename = RequestStringInput(
"Enter the private key filename");
std::ifstream public_key_file(public_key_filename);
std::stringstream public_key_buffer;
public_key_buffer << public_key_file.rdbuf();
*rsa_public_key = public_key_buffer.str();
std::ifstream private_key_file(private_key_filename);
std::stringstream private_key_buffer;
private_key_buffer << private_key_file.rdbuf();
*rsa_private_key = private_key_buffer.str();
return WaitForServerResponse::kDisabled;
}
WaitForServerResponse HandleSaveRsaKeysOperation(
const std::string& rsa_public_key, const std::string& rsa_private_key) {
auto public_key_filename = RequestStringInput(
"Enter the public key filename");
auto private_key_filename = RequestStringInput(
"Enter the private key filename");
std::ofstream public_key_file(public_key_filename);
public_key_file << rsa_public_key;
std::ofstream private_key_file(private_key_filename);
private_key_file << rsa_private_key;
return WaitForServerResponse::kDisabled;
}
WaitForServerResponse HandleSendRsaPublicKeyOperation(
int socket_fd, const std::string& rsa_public_key) {
if (rsa_public_key.empty()) {
std::cout << "Please load or generate key pair firstly." << std::endl;
return WaitForServerResponse::kDisabled;
}
RsaPublicKey rsa_public_key_proto;
rsa_public_key_proto.set_key(rsa_public_key);
Message message_to_send;
*message_to_send.mutable_rsa_public_key() = std::move(rsa_public_key_proto);
auto status = SendUnencryptedMessage(message_to_send, socket_fd);
if (!status.ok()) {
std::cout << status << std::endl;
return WaitForServerResponse::kDisabled;
}
return WaitForServerResponse::kEnabled;
}
WaitForServerResponse HandleUpdateSessionKeyOperation(int socket_fd) {
SessionKey session_key_proto;
Message message_to_send;
*message_to_send.mutable_session_key() = std::move(session_key_proto);
auto status = SendUnencryptedMessage(message_to_send, socket_fd);
if (!status.ok()) {
std::cout << status << std::endl;
return WaitForServerResponse::kDisabled;
}
return WaitForServerResponse::kEnabled;
}
WaitForServerResponse HandleSetPasswordOperation(
std::string* password_hash_key) {
auto password = RequestStringInput("Enter new password");
*password_hash_key = GetSha256Hash(password);
return WaitForServerResponse::kDisabled;
}
WaitForServerResponse HandleResetPasswordOperation(
std::string* password_hash_key) {
*password_hash_key = "dshflshfg7598vn7435342nqwe57vnw5";
return WaitForServerResponse::kDisabled;
}
WaitForServerResponse HandleGetDataOperation(
int socket_fd, const std::string& aes_encryption_key) {
if (aes_encryption_key.empty()) {
std::cout << "Please update yor session key firstly." << std::endl;
return WaitForServerResponse::kDisabled;
}
auto key = GetSha256Hash(RequestStringInput("Enter the key"));
DataOperation data_operation_proto;
data_operation_proto.set_type(DataOperation::GET);
data_operation_proto.set_key(key);
Message message_to_send;
*message_to_send.mutable_data_operation() = std::move(data_operation_proto);
auto status = EncryptAndSendMessage(
message_to_send, socket_fd, aes_encryption_key);
if (!status.ok()) {
std::cout << status << std::endl;
return WaitForServerResponse::kDisabled;
}
return WaitForServerResponse::kEnabled;
}
WaitForServerResponse HandleUpdateDataOperation(
int socket_fd,
const std::string& aes_encryption_key,
const std::string& password_hash_key) {
if (aes_encryption_key.empty()) {
std::cout << "Please update yor session key firstly." << std::endl;
return WaitForServerResponse::kDisabled;
}
auto key = GetSha256Hash(RequestStringInput("Enter the key"));
auto content = RequestStringInput("Enter the content (line)");
auto content_encryption_init_vector = Generate128BitKey();
auto encrypted_content = EncryptStringWithAesCbcCipher(
content, password_hash_key, content_encryption_init_vector);
if (!encrypted_content.ok()) {
std::cout << encrypted_content.status() << std::endl;
return WaitForServerResponse::kDisabled;
}
DataOperation data_operation_proto;
data_operation_proto.set_type(DataOperation::UPDATE);
data_operation_proto.set_key(key);
data_operation_proto.set_content(*encrypted_content);
data_operation_proto.set_content_encryption_init_vector(
content_encryption_init_vector);
Message message_to_send;
*message_to_send.mutable_data_operation() = std::move(data_operation_proto);
auto status = EncryptAndSendMessage(
message_to_send, socket_fd, aes_encryption_key);
if (!status.ok()) {
std::cout << status << std::endl;
return WaitForServerResponse::kDisabled;
}
return WaitForServerResponse::kEnabled;
}
WaitForServerResponse HandleDeleteDataOperation(
int socket_fd, const std::string& aes_encryption_key) {
if (aes_encryption_key.empty()) {
std::cout << "Please update yor session key firstly." << std::endl;
return WaitForServerResponse::kDisabled;
}
auto key = GetSha256Hash(RequestStringInput("Enter the key"));
DataOperation data_operation_proto;
data_operation_proto.set_type(DataOperation::DELETE);
data_operation_proto.set_key(key);
Message message_to_send;
*message_to_send.mutable_data_operation() = std::move(data_operation_proto);
auto status = EncryptAndSendMessage(
message_to_send, socket_fd, aes_encryption_key);
if (!status.ok()) {
std::cout << status << std::endl;
return WaitForServerResponse::kDisabled;
}
return WaitForServerResponse::kEnabled;
}
} // namespace
constexpr absl::string_view kProgramFeaturesPrompt = R"(
Choose an operation from the following:
- generate_rsa_keys / load_rsa_keys / save_rsa_keys
- send_rsa_public_key
- update_session_key
- set_password / reset_password
- get_data / update_data / delete_data
- exit)";
constexpr absl::string_view kProgramRunningHelpText = R"(
Arguments format:
./client [server ip] [server port]
Example:
./client 127.0.0.1 8701)";
int main(int argc, char** argv) {
if (argc != 3) {
std::cout << "Invalid arguments.\n" << kProgramRunningHelpText << '\n';
return 1;
}
const std::string server_ip = argv[1];
const uint16_t server_port = std::stoi(argv[2]);
int socket_fd = socket(AF_INET, SOCK_STREAM, 6);
if (socket_fd == -1) {
perror("Error creating socket");
return 1;
}
const sockaddr_in server_address = {
.sin_family = AF_INET,
.sin_port = htons(server_port),
.sin_addr = {inet_addr(server_ip.c_str())}};
if (connect(socket_fd,
reinterpret_cast<const sockaddr*>(&server_address),
sizeof(sockaddr_in)) == -1) {
perror("Error establishing connection");
return 1;
}
std::string rsa_public_key;
std::string rsa_private_key;
std::string aes_encryption_key;
std::string password_hash_key;
HandleResetPasswordOperation(&password_hash_key);
while (true) {
auto operation = RequestStringInput(kProgramFeaturesPrompt);
WaitForServerResponse status;
if (operation == "exit") {
break;
} else if (operation == "generate_rsa_keys") {
status = HandleGenerateRsaKeysOperation(
&rsa_public_key, &rsa_private_key);
} else if (operation == "load_rsa_keys") {
status = HandleLoadRsaKeysOperation(&rsa_public_key, &rsa_private_key);
} else if (operation == "save_rsa_keys") {
status = HandleSaveRsaKeysOperation(rsa_public_key, rsa_private_key);
} else if (operation == "send_rsa_public_key") {
status = HandleSendRsaPublicKeyOperation(socket_fd, rsa_public_key);
} else if (operation == "update_session_key") {
status = HandleUpdateSessionKeyOperation(socket_fd);
} else if (operation == "set_password") {
status = HandleSetPasswordOperation(&password_hash_key);
} else if (operation == "reset_password") {
status = HandleResetPasswordOperation(&password_hash_key);
} else if (operation == "get_data") {
status = HandleGetDataOperation(socket_fd, aes_encryption_key);
} else if (operation == "update_data") {
status = HandleUpdateDataOperation(
socket_fd, aes_encryption_key, password_hash_key);
} else if (operation == "delete_data") {
status = HandleDeleteDataOperation(socket_fd, aes_encryption_key);
} else {
std::cout << "Unsupported operation." << std::endl;
continue;
}
if (status == WaitForServerResponse::kDisabled) {
continue;
}
auto received_message =
ReceiveAndDecryptMessage(socket_fd, aes_encryption_key);
if (!received_message.ok()) {
std::cout << received_message.status();
if (absl::IsCancelled(received_message.status())) {
break;
}
continue;
}
if (received_message->has_session_key()) {
std::cout << "Received a message with the session key." << std::endl;
auto& encrypted_encryption_key =
received_message->session_key().encryption_key();
auto encryption_key = DecryptStringWithRsaPrivateKey(
encrypted_encryption_key, rsa_private_key);
if (!encryption_key.ok()) {
std::cout << encryption_key.status() << std::endl;
} else {
aes_encryption_key = *encryption_key;
}
continue;
}
if (received_message->has_info()) {
std::cout << "Received a message with info of status "
<< Info::Status_Name(received_message->info().status())
<< " and description '"
<< received_message->info().description() << "'."
<< std::endl;
continue;
}
if (received_message->has_data_operation()) {
std::cout << "Received a message with the content" << std::endl;
auto decrypted_content = DecryptStringWithAesCbcCipher(
received_message->data_operation().content(), password_hash_key,
received_message->data_operation().content_encryption_init_vector());
if (!decrypted_content.ok()) {
std::cout << decrypted_content.status() << std::endl;
std::cout
<< "Probably, you need to use another password to correctly decrypt this data."
<< std::endl;
} else {
std::cout << *decrypted_content << std::endl;
}
continue;
}
}
std::cout << "Shutting connection down" << std::endl;
if (shutdown(socket_fd, SHUT_RDWR) == -1) {
perror("Error shutting socket down");
if (close(socket_fd) == -1) {
perror("Error closing socket");
}
return 1;
}
if (close(socket_fd) == -1) {
perror("Error closing socket");
return 1;
}
return 0;
}