-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBank_Server.cpp
More file actions
794 lines (661 loc) · 26 KB
/
Bank_Server.cpp
File metadata and controls
794 lines (661 loc) · 26 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//server
#include <iostream>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <map>
#include <sstream>
#include <vector>
#include <cstring>
#include <random>
#include <chrono>
#include <openssl/sha.h>
#include <regex>
#include <mutex>
#include <thread>
#include <fstream>
#include <openssl/aes.h>
#include <iomanip>
using namespace std;
// Add at the top of server.cpp
struct AccountData {
string username;
string password;
string card_id;
string account_number;
double balance;
};
struct Transaction {
string account_number;
string type; // "DEPOSIT" or "WITHDRAW"
double amount;
string timestamp;
};
// Global data structures
map<string, AccountData> accounts;
vector<Transaction> transactions;
void save_auth_file(const string &filename);
#define PRIVATE_KEY_FILE "../../certs/private_key.pem"
#define AUTH_FILE "../auth.txt"
#define BACKUP_FILE "../auth_backup.txt"
map<string, tuple<string, double, string, string>> user_database;
mutex user_database_mutex;
map<string, string> session_tokens;
mutex session_tokens_mutex;
const int SESSION_TIMEOUT_SECONDS = 10;
map<string, chrono::steady_clock::time_point> session_last_activity;
mutex session_last_activity_mutex;
string server_ip = "127.0.0.1"; // Default to all interfaces
int server_port = 8080; // Default port
void log_message(const string &msg) {
cout << "[INFO] " << msg << "\n";
}
void log_error(const string &msg) {
cerr << "[ERROR] " << msg << "\n";
}
bool is_valid_ip(const string &ip) {
// Check if IP is empty or localhost
if (ip.empty() || ip == "localhost" || ip == "127.0.0.1") {
return true;
}
// Basic format check using regex
regex ip_regex("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
if (!regex_match(ip, ip_regex)) {
return false;
}
// Additional validation for special cases
if (ip == "0.0.0.0") {
return false;
}
return true;
}
void get_server_details() {
cout << "Enter server IP (default: 127.0.0.1): ";
string input_ip;
getline(cin, input_ip);
if (!input_ip.empty()) {
if (!is_valid_ip(input_ip)) {
log_error("Invalid IP format. Using default: 127.0.0.1");
server_ip = "127.0.0.1";
} else {
server_ip = input_ip;
}
} else {
server_ip = "127.0.0.1"; // Default to localhost
}
cout << "Enter server port (default: 8080): ";
string input_port;
getline(cin, input_port);
if (!input_port.empty()) {
try {
int port = stoi(input_port);
if (port > 0 && port < 65536) {
server_port = port;
} else {
log_error("Invalid port number (must be between 1-65535). Using default: 8080");
server_port = 8080;
}
} catch (const exception&) {
log_error("Invalid port format. Using default: 8080");
server_port = 8080;
}
} else {
server_port = 8080;
}
log_message("Server will listen on " + server_ip + ":" + to_string(server_port));
}
bool is_valid_username(const string &username) {
// Check for spaces first
if (username.find(' ') != string::npos) {
return false;
}
if (username == "." || username == "..") {
return true;
}
regex username_regex(R"(^(?!.[-]{2,})(?!.[.]{2,})[a-z0-9_.-]{1,122}$)");
if (!regex_match(username, username_regex)) {
return false;
}
return true;
}
bool is_valid_amount(const string &amount_str) {
regex amount_regex(R"(^([1-9][0-9]*|0)\.[0-9]{2}$)");
if (!regex_match(amount_str, amount_regex)) {
return false;
}
try {
size_t decimal_pos = amount_str.find('.');
if (decimal_pos == string::npos ||
amount_str.length() - decimal_pos - 1 != 2) {
return false;
}
double amount = stod(amount_str);
return amount > 0.00 && amount <= 4294967295.99;
} catch (const exception&) {
return false;
}
}
void handle_error(SSL *ssl, const string &error_message) {
log_error(error_message);
SSL_write(ssl, "An error occurred.", 42);
}
string base64_decode(const string &input) {
BIO *b64 = BIO_new(BIO_f_base64());
BIO *bmem = BIO_new_mem_buf(input.c_str(), input.size());
b64 = BIO_push(b64, bmem);
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
vector<unsigned char> output(input.size());
int len = BIO_read(b64, output.data(), input.size());
BIO_free_all(b64);
return string(output.begin(), output.begin() + len);
}
string rsa_decrypt(const string &encrypted_base64) {
string encrypted = base64_decode(encrypted_base64);
FILE *privkey_file = fopen(PRIVATE_KEY_FILE, "rb");
if (!privkey_file) {
log_error("Failed to open private key file");
return "";
}
EVP_PKEY *privkey = PEM_read_PrivateKey(privkey_file, NULL, NULL, NULL);
fclose(privkey_file);
if (!privkey) {
log_error("Failed to read private key");
return "";
}
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(privkey, NULL);
EVP_PKEY_decrypt_init(ctx);
size_t outlen = 0;
EVP_PKEY_decrypt(ctx, NULL, &outlen, (const unsigned char *)encrypted.c_str(), encrypted.size());
vector<unsigned char> decrypted(outlen);
if (EVP_PKEY_decrypt(ctx, decrypted.data(), &outlen, (const unsigned char *)encrypted.c_str(), encrypted.size()) <= 0) {
log_error("Decryption failed");
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(privkey);
return "";
}
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(privkey);
return string(decrypted.begin(), decrypted.end());
}
string generate_random_number(int length) {
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(0, 9);
string number;
for (int i = 0; i < length; ++i) {
number += to_string(dis(gen));
}
return number;
}
string hash_password_server(const string &password) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256((unsigned char *)password.c_str(), password.size(), hash);
return string((char*)hash, SHA256_DIGEST_LENGTH);
}
string generate_session_token() {
string token = generate_random_number(32);
return token;
}
void handle_protocol_error(SSL *ssl) {
log_error("protocol error");
SSL_write(ssl, "protocol error", 14);
}
void handle_create_account(SSL *ssl, const string &username, const string &encrypted_password, const string &initial_amount_str) {
if (!is_valid_username(username)) {
string error_msg = "ERROR 255 - due to invalid user name";
SSL_write(ssl, error_msg.c_str(), error_msg.size());
log_error("Account creation failed: invalid username - " + username);
return;
}
if (!is_valid_amount(initial_amount_str)) {
string error_msg = "ERROR 255 - due to invalid amount value";
SSL_write(ssl, error_msg.c_str(), error_msg.size());
log_error("Account creation failed: invalid amount - " + initial_amount_str + " for username: " + username);
return;
}
double initial_amount = stod(initial_amount_str);
if (initial_amount < 10.00) {
string error_msg = "ERROR 255 - due to invalid amount value";
SSL_write(ssl, error_msg.c_str(), error_msg.size());
log_error("Account creation failed: initial amount less than 10 for username: " + username);
return;
}
string decrypted_password = rsa_decrypt(encrypted_password);
if (decrypted_password.empty()) {
handle_protocol_error(ssl);
return;
}
string hashed_password = hash_password_server(decrypted_password);
if (hashed_password.empty()) {
handle_protocol_error(ssl);
return;
}
lock_guard<mutex> lock(user_database_mutex);
if (user_database.find(username) != user_database.end()) {
string error_msg = "ERROR 255 - due to account already exists";
SSL_write(ssl, error_msg.c_str(), error_msg.size());
log_error("Account creation failed: username already exists");
} else {
string card_id = generate_random_number(16);
string account_number = generate_random_number(10);
user_database[username] = {hashed_password, initial_amount, card_id, account_number};
ostringstream json_response;
json_response << "{"
<< "\"account\":\"" << account_number << "\","
<< "\"card_id\":\"" << card_id << "\","
<< "\"initial_balance\":" << fixed << setprecision(2) << initial_amount
<< "}";
string response = json_response.str();
SSL_write(ssl, response.c_str(), response.size());
log_message("Account created for username: " + username +
" with initial amount: " + initial_amount_str +
" card_id: " + card_id +
" account_number: " + account_number);
save_auth_file(AUTH_FILE);
}
}
bool handle_login(SSL *ssl, const string &username, const string &encrypted_password, const string &card_id, const string &account_number) {
if (!is_valid_username(username)) {
handle_protocol_error(ssl);
return false;
}
string decrypted_password = rsa_decrypt(encrypted_password);
string hashed_password = hash_password_server(decrypted_password);
lock_guard<mutex> lock(user_database_mutex);
if (user_database.find(username) != user_database.end()) {
auto &[stored_password, balance, stored_card_id, stored_account_number] = user_database[username];
if (stored_password == hashed_password && stored_card_id == card_id && stored_account_number == account_number) {
string session_token = generate_session_token();
{
lock_guard<mutex> session_lock(session_tokens_mutex);
session_tokens[username] = session_token;
}
{
lock_guard<mutex> activity_lock(session_last_activity_mutex);
session_last_activity[username] = chrono::steady_clock::now();
}
string response = "LOGIN_SUCCESS " + session_token;
SSL_write(ssl, response.c_str(), response.size());
log_message("Login successful for username: " + username);
return true;
} else {
SSL_write(ssl, "LOGIN_FAILED: Incorrect credentials", 36);
log_error("Login failed: incorrect credentials for " + username);
}
} else {
SSL_write(ssl, "LOGIN_FAILED: User not found", 28);
log_error("Login failed: user not found - " + username);
}
return false;
}
bool is_session_valid(const string &username) {
lock_guard<mutex> lock(session_last_activity_mutex);
auto it = session_last_activity.find(username);
if (it == session_last_activity.end()) {
return true;
}
auto now = chrono::steady_clock::now();
auto duration = chrono::duration_cast<chrono::seconds>(now - it->second).count();
return duration <= SESSION_TIMEOUT_SECONDS;
}
void update_session_activity(const string &username) {
lock_guard<mutex> lock(session_last_activity_mutex);
session_last_activity[username] = chrono::steady_clock::now();
}
void handle_transaction(SSL *ssl, const string &username) {
char buffer[1024] = {0};
double previous_balance = 0.0;
bool transaction_in_progress = false;
while (true) {
int bytes_read = SSL_read(ssl, buffer, sizeof(buffer));
if (bytes_read <= 0) {
if (transaction_in_progress) {
lock_guard<mutex> lock(user_database_mutex);
get<1>(user_database[username]) = previous_balance;
save_auth_file(AUTH_FILE);
}
log_error("Failed to read from SSL connection");
break;
}
if (!is_session_valid(username)) {
SSL_write(ssl, "ERROR 255 - Session expired", 28);
log_error("Session expired for username: " + username);
break;
}
string command(buffer, bytes_read);
log_message("Received command: " + command);
istringstream iss(command);
string session_token, action, amount_str;
iss >> session_token >> action;
{
lock_guard<mutex> lock(session_tokens_mutex);
if (session_tokens[username] != session_token) {
string error_msg = "ERROR 255 - due to invalid session token";
SSL_write(ssl, error_msg.c_str(), error_msg.size());
log_error("Invalid session token for username: " + username);
// Clear session data
session_tokens.erase(username);
{
lock_guard<mutex> activity_lock(session_last_activity_mutex);
session_last_activity.erase(username);
}
// Force client to return to main menu
return; // This will exit handle_transaction and return to main loop
}
}
update_session_activity(username);
if (action == "LOGOUT") {
{
lock_guard<mutex> lock(session_tokens_mutex);
session_tokens.erase(username);
}
string response = "Logged out successfully";
SSL_write(ssl, response.c_str(), response.size());
log_message("User logged out: " + username);
break;
} else if (action == "CHECK_BALANCE") {
lock_guard<mutex> lock(user_database_mutex);
double balance = get<1>(user_database[username]);
ostringstream json_response;
json_response << "{"
<< "\"account\":\"" << get<3>(user_database[username]) << "\","
<< "\"balance\":" << fixed << setprecision(2) << balance
<< "}";
string response = json_response.str();
SSL_write(ssl, response.c_str(), response.size());
log_message("Balance checked for username: " + username + ", Balance: " + to_string(balance));
} else if (action == "DEPOSIT" || action == "WITHDRAW") {
iss >> amount_str;
if (!is_valid_amount(amount_str)) {
string error_msg = "ERROR 255 - due to invalid amount value";
SSL_write(ssl, error_msg.c_str(), error_msg.size());
log_error("Transaction failed: invalid amount - " + amount_str + " for username: " + username);
continue;
}
double amount = stod(amount_str);
try {
lock_guard<mutex> lock(user_database_mutex);
if (!transaction_in_progress) {
previous_balance = get<1>(user_database[username]);
transaction_in_progress = true;
}
if (action == "DEPOSIT") {
get<1>(user_database[username]) += amount;
ostringstream json_response;
json_response << "{"
<< "\"account\":\"" << get<3>(user_database[username]) << "\","
<< "\"deposit\":" << fixed << setprecision(2) << amount
<< "}";
string response = json_response.str();
SSL_write(ssl, response.c_str(), response.size());
log_message("Deposit made for username: " + username + ", Amount: " + amount_str);
} else {
if (get<1>(user_database[username]) >= amount) {
get<1>(user_database[username]) -= amount;
ostringstream json_response;
json_response << "{"
<< "\"account\":\"" << get<3>(user_database[username]) << "\","
<< "\"withdraw\":" << fixed << setprecision(2) << amount
<< "}";
string response = json_response.str();
SSL_write(ssl, response.c_str(), response.size());
log_message("Withdrawal made for username: " + username + ", Amount: " + amount_str);
} else {
string error_msg = "ERROR 255 - due to insufficient balance";
SSL_write(ssl, error_msg.c_str(), error_msg.size());
log_error("Insufficient balance for username: " + username);
continue;
}
}
save_auth_file(AUTH_FILE);
transaction_in_progress = false;
} catch (const exception&) {
string error_msg = "ERROR 255 - due to invalid amount value";
SSL_write(ssl, error_msg.c_str(), error_msg.size());
log_error("Exception occurred while processing amount: " + amount_str);
}
} else {
string error_msg = "ERROR 255 - due to invalid command";
SSL_write(ssl, error_msg.c_str(), error_msg.size());
log_error("Invalid command received: " + action);
}
memset(buffer, 0, sizeof(buffer));
}
if (transaction_in_progress) {
lock_guard<mutex> lock(user_database_mutex);
get<1>(user_database[username]) = previous_balance;
save_auth_file(AUTH_FILE);
}
}
SSL_CTX *create_ssl_context() {
const SSL_METHOD *method = TLS_server_method();
SSL_CTX *ctx = SSL_CTX_new(method);
if (!ctx) {
log_error("Unable to create SSL context");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
// Load the server certificate and private key
if (SSL_CTX_use_certificate_file(ctx, "../../certs/server_cert.pem", SSL_FILETYPE_PEM) <= 0) {
log_error("Failed to load server certificate");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
if (SSL_CTX_use_PrivateKey_file(ctx, "../../certs/server_key.pem", SSL_FILETYPE_PEM) <= 0) {
log_error("Failed to load server private key");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
// Set verify mode to require client certificate
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
SSL_CTX_set_verify_depth(ctx, 1);
// Add this line to load CA certificate for client verification
if (SSL_CTX_load_verify_locations(ctx, "../../certs/ca_cert.pem", NULL) <= 0) {
log_error("Failed to load CA certificate for client verification");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
// Verify private key
if (!SSL_CTX_check_private_key(ctx)) {
log_error("Private key does not match the certificate");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
return ctx;
}
void derive_aes_key_iv(const string &password, unsigned char *aes_key, unsigned char *aes_iv) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256((unsigned char*)password.c_str(), password.size(), hash);
memcpy(aes_key, hash, 32);
memcpy(aes_iv, hash + 16, 16);
}
string aes_decrypt(const string &ciphertext, const unsigned char *aes_key, const unsigned char *aes_iv) {
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, aes_key, aes_iv);
vector<unsigned char> plaintext(ciphertext.size());
int len, plaintext_len = 0;
EVP_DecryptUpdate(ctx, plaintext.data(), &len, (unsigned char*)ciphertext.c_str(), ciphertext.size());
plaintext_len += len;
EVP_DecryptFinal_ex(ctx, plaintext.data() + len, &len);
plaintext_len += len;
EVP_CIPHER_CTX_free(ctx);
return string(plaintext.begin(), plaintext.begin() + plaintext_len);
}
void save_auth_file(const string &filename) {
ofstream file(filename);
if (!file.is_open()) {
log_error("Failed to open auth file for writing: " + filename);
return;
}
for (const auto &[username, data] : user_database) {
const auto &[password, balance, card_id, account_number] = data;
file << username << " " << password << " " << balance << " " << card_id << " " << account_number << "\n";
}
file.close();
log_message("Auth file saved: " + filename);
}
void load_auth_file(const string &filename) {
ifstream file(filename);
if (!file.is_open()) {
log_error("Failed to open auth file for reading: " + filename);
return;
}
user_database.clear();
string line;
while (getline(file, line)) {
istringstream iss(line);
string username, password, card_id, account_number;
double balance;
if (iss >> username >> password >> balance >> card_id >> account_number) {
user_database[username] = {password, balance, card_id, account_number};
}
}
file.close();
log_message("Auth file loaded: " + filename);
}
void create_backup() {
save_auth_file(BACKUP_FILE);
log_message("Backup created: " + string(BACKUP_FILE));
}
void save_accounts_to_file() {
ofstream file("bank_accounts.dat", ios::out);
if (!file) {
log_error("Failed to open accounts file for writing");
return;
}
for (const auto& [username, data] : accounts) {
file << username << "|"
<< data.password << "|"
<< data.card_id << "|"
<< data.account_number << "|"
<< fixed << setprecision(2) << data.balance << "\n";
}
file.close();
}
void load_accounts_from_file() {
ifstream file("bank_accounts.dat");
if (!file) {
log_message("No existing accounts file found");
return;
}
string line;
while (getline(file, line)) {
stringstream ss(line);
string username, password, card_id, account_number, balance_str;
getline(ss, username, '|');
getline(ss, password, '|');
getline(ss, card_id, '|');
getline(ss, account_number, '|');
getline(ss, balance_str, '|');
AccountData data;
data.username = username;
data.password = password;
data.card_id = card_id;
data.account_number = account_number;
data.balance = stod(balance_str);
accounts[username] = data;
}
file.close();
}
int main() {
SSL_CTX *ctx = create_ssl_context();
load_auth_file(AUTH_FILE);
create_backup();
get_server_details();
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
log_error("Socket creation failed");
return -1;
}
// Add this to allow socket reuse
int opt = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
log_error("Setsockopt failed");
return -1;
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(server_port);
if (inet_pton(AF_INET, server_ip.c_str(), &addr.sin_addr) <= 0) {
log_error("Invalid address/Address not supported");
return -1;
}
if (bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
log_error("Bind failed");
return -1;
}
listen(server_fd, SOMAXCONN);
log_message("Server listening on port " + to_string(server_port) + "...");
thread session_cleanup_thread([]() {
while (true) {
this_thread::sleep_for(chrono::seconds(60));
lock_guard<mutex> lock(session_last_activity_mutex);
auto now = chrono::steady_clock::now();
for (auto it = session_last_activity.begin(); it != session_last_activity.end();) {
if (chrono::duration_cast<chrono::seconds>(now - it->second).count() > SESSION_TIMEOUT_SECONDS) {
lock_guard<mutex> lock(session_tokens_mutex);
session_tokens.erase(it->first);
it = session_last_activity.erase(it);
} else {
++it;
}
}
}
});
// Load accounts data at startup
load_accounts_from_file();
while (true) {
int client_fd = accept(server_fd, NULL, NULL);
thread([client_fd, ctx]() {
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, client_fd);
if (SSL_accept(ssl) <= 0) {
log_error("SSL accept failed");
ERR_print_errors_fp(stderr);
SSL_free(ssl);
close(client_fd);
return;
}
// Add this block for certificate verification
X509* client_cert = SSL_get_peer_certificate(ssl);
if (client_cert == NULL) {
log_error("No client certificate provided");
SSL_free(ssl);
close(client_fd);
return;
}
if (SSL_get_verify_result(ssl) != X509_V_OK) {
log_error("Client certificate verification failed");
X509_free(client_cert);
SSL_free(ssl);
close(client_fd);
return;
}
X509_free(client_cert);
char buffer[1024] = {0};
SSL_read(ssl, buffer, sizeof(buffer));
log_message("Received message: " + string(buffer));
string command, username, encrypted_password, card_id, account_number, initial_amount_str;
istringstream iss(buffer);
iss >> command >> username >> encrypted_password;
if (command == "CREATE_ACCOUNT") {
iss >> initial_amount_str;
handle_create_account(ssl, username, encrypted_password, initial_amount_str);
} else if (command == "LOGIN") {
iss >> card_id >> account_number;
if (handle_login(ssl, username, encrypted_password, card_id, account_number)) {
handle_transaction(ssl, username);
}
}
SSL_shutdown(ssl);
SSL_free(ssl);
close(client_fd);
}).detach();
create_backup();
}
close(server_fd);
SSL_CTX_free(ctx);
return 0;
}