-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.cpp
2523 lines (2276 loc) · 76.3 KB
/
client.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
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <vector>
#include <climits>
#include <limits>
#include <unistd.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <sstream>
#include "constant.h"
#include "util.h"
#include "crypto.h"
using namespace std;
//---------------- GLOBAL VARIABLES ------------------//
/* This global variable is setted to true if the user is in chat with
* another client, to false otherwise*/
bool isChatting = false;
/* This global variable is setted to true when an error occurs*/
bool error = false;
/* Username of the "logged" user*/
string loggedUser;
/* Id of the logged user */
int loggedUser_id;
/* ID and username of the user that I am chatting with */
string peer_username;
int peer_id;
/* socket id*/
int sock_id;
/* Session key between client and server*/
unsigned char* session_key_clientToServer = NULL;
uint32_t session_key_clientToServer_len = 0;
/* Session key between client and client*/
unsigned char* session_key_clientToClient = NULL;
uint32_t session_key_clientToClient_len = 0;
/* Peer Public Key*/
unsigned char* peer_pub_key = NULL;
/* Server certificate */
unsigned char* server_cert = NULL;
// Counter for freshness
uint32_t receive_counter=0;
uint32_t send_counter=0;
uint32_t receive_counter_client_client = 0;
uint32_t send_counter_client_client = 0;
//---------------- STRUCTURES ------------------//
struct commandMSG
{
uint8_t opcode;
int userId;
};
struct genericMSG
{
uint8_t opcode;
uint16_t user_id_recipient;
uint16_t length;
unsigned char* payload;
};
struct user
{
int userId;
unsigned char* username;
size_t usernameSize;
user* next;
};
/* pointer to the list of online users*/
user* user_list = NULL;
/**
* @brief Print the welcome message
*
*/
void welcome()
{
cout << " *********************************************************************** " << endl;
cout << " SECURE COMMUNICATION " << endl;
cout << " *********************************************************************** " << endl;
cout << " !exit Close the application" << endl;
cout << " !help See all the possible commands" << endl;
cout << "-------------------------------------------------------------------------" << endl;
}
/**
* @brief Print the help command
*
*/
void help()
{
cout << "\n*********************************************************************" << endl;
cout << " !users_online" << endl;
cout << " Ask the server to return the list of the online users" << endl;
cout << " !chat" << endl;
cout << " Ask the server to start a chat" << endl;
cout << " !exit" << endl;
cout << " Close the application" << endl;
cout << "*********************************************************************\n" << endl;
}
/**
* @brief Command handler
*
* @param cmd string which is the command
* @return uint8_t opcode
*/
uint8_t commandStringHandler(string cmd)
{
if(cmd.compare("!exit")==0)
return EXIT_CMD;
else if(cmd.compare("!users_online")==0)
return ONLINE_CMD;
else if(cmd.compare("!chat")==0)
return CHAT_CMD;
else if(cmd.compare("!help")==0)
return HELP_CMD;
else if(cmd.compare("!stop_chat")==0)
return STOP_CHAT;
else
return NOT_VALID_CMD;
}
/**
* @brief Get the Username From the user id
*
* @param userId
* @param userlist
* @return string that is the username, empty string if error
*/
string getUsernameFromID(int userId, user* userlist)
{
if(userlist==NULL) {
cout << " Warning: userlist is null " << endl;
return string();
}
struct user* tmp = userlist;
while(tmp!=NULL) {
if(tmp->userId==userId) {
string username ((char*)(tmp->username));
return username;
}
tmp = tmp->next;
}
return string();
}
/**
* @brief Handle the client side part of the command chat
*
* @param toSend
* @return int -1 requested user is not in the userlist or userlist is empty, 0 otherwise
*/
int chat(struct commandMSG* toSend, user* userlist)
{
if(userlist==NULL || toSend==NULL)
return -1;
toSend->opcode = CHAT_CMD;
cout << "\n******************************************************" << endl;
cout << "Write the userID of the user that you want to contact" << endl;
printf(" > ");
cin >> toSend->userId;
if(cin.fail()){
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return -1;
}
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(toSend->userId==loggedUser_id){
cout << " You cannot chat with yourself " << endl;
return -1;
}
if(toSend->userId<0){
cout << " Negative user id " << endl;
return -1;
}
cout << "Wait for user's response and authentication ...." << endl;
peer_id = toSend->userId;
peer_username = getUsernameFromID(peer_id, userlist);
if(peer_username.empty())
return -1;
return 0;
}
/**
* @brief Free the list of users
*
* @param userlist head of the list that must be cleaned
*/
void free_list_users(struct user* userlist)
{
if(userlist==NULL)
return;
struct user* toDelete = userlist;
struct user* nextDeletion = NULL;
while(toDelete!=NULL) {
nextDeletion = toDelete->next;
free(toDelete->username);
free(toDelete);
toDelete = nextDeletion;
}
}
/**
* @brief The function receives from the server the list of the user and it store it
*
* @param plaintext received message decrypted
* @param pt_len length of the message decrypted
* @return The number of online users, -1 if error, 0 if no user in the list
*/
int retrieveOnlineUsers(unsigned char* plaintext, uint32_t pt_len)
{
if(plaintext==NULL)
return -1;
if(user_list!=NULL){
free_list_users(user_list);
user_list = NULL;
}
uint32_t howMany;
int ret;
uint32_t bytes_read = 5; // Because I have already read the opcode and the seq number
// Read how many users
memcpy(&howMany, plaintext+bytes_read, sizeof(uint32_t));
bytes_read += sizeof(uint32_t);
howMany = ntohl(howMany);
if(ret <= 0)
return -1;
if(howMany==0)
return 0;
if(howMany>REGISTERED_USERS)
return -1;
struct user* current = NULL;
struct user* tmp = NULL;
for(int i = 0; i<howMany; i++) {
int username_size;
tmp = (struct user*)malloc(sizeof(user));
if(!tmp) {
errorHandler(MALLOC_ERR);
free_list_users(user_list);
user_list = NULL;
return -1;
}
tmp->username = NULL;
tmp->userId = -1;
tmp->next = NULL;
tmp->usernameSize = 0;
memcpy(&(tmp->userId), plaintext+bytes_read, sizeof(int));
bytes_read += sizeof(int);
tmp->userId = ntohl(tmp->userId);
if(ret <= 0) {
free(tmp);
free_list_users(user_list);
user_list = NULL;
return -1;
}
memcpy(&username_size, plaintext+bytes_read, sizeof(int));
bytes_read += sizeof(int);
username_size = ntohl(username_size);
tmp->usernameSize = username_size;
if(username_size>MAX_USERNAME_SIZE) {
free(tmp);
free_list_users(user_list);
user_list = NULL;
return -1;
}
tmp->username = (unsigned char*)malloc(username_size+1);
if(!tmp->username){
errorHandler(MALLOC_ERR);
free(tmp);
free_list_users(user_list);
user_list = NULL;
return -1;
}
if(bytes_read+username_size>pt_len){
cerr << " Error in reading plaintext " << endl;
free(tmp);
free_list_users(user_list);
user_list = NULL;
return -1;
}
memcpy(tmp->username, plaintext+bytes_read, username_size);
bytes_read += username_size;
tmp->username[username_size] = '\0';
if(i==0)
user_list = tmp;
else
current->next = tmp;
current = tmp;
}
return howMany;
}
/**
* @brief Printf the list of users
*
* @param userlist The list of the user that I have to print
* @return -1 in case of error, 0 otherwise.
*/
int print_list_users(user* userlist)
{
if(userlist==NULL) {
cout << " User list is NULL " << endl;
return -1;
}
struct user* tmp = userlist;
cout << endl;
cout << "**** USER LIST **** " << endl;
cout << " ID \t Username" << endl;
while(tmp!=NULL) {
cout << " " << tmp->userId << " \t " << tmp->username << endl;
tmp = tmp->next;
}
cout << "****************** " << endl;
cout << endl;
return 0;
}
/**
* @brief Retrieve the plaintext from the encrypted message
*
* @param ciphertext ciphertext
* @param ct_len ciphertext length
* @param plaintext plaintext
* @return Return plaintext len or -1 in case of error
*/
int open_msg_by_client(unsigned char* ciphertext, uint32_t msgRecLen, unsigned char** plaintext)
{
if(ciphertext==NULL)
return -1;
uint32_t header_len = sizeof(uint32_t)+IV_DEFAULT+TAG_DEFAULT;
uint32_t read = 9; // because seq number, opcode and len already read
uint32_t ct_len;
uint32_t pt_len;
int ret;
unsigned char* header = (unsigned char*)malloc(header_len);
if(!header){
cerr << " Error in malloc for header " << endl;
return -1;
}
memcpy(header, ciphertext+read, header_len);
read += header_len;
unsigned char* iv = (unsigned char*)malloc(IV_DEFAULT);
if(!iv){
cerr << " Error in malloc for iv " << endl;
free(header);
return -1;
}
unsigned char* tag = (unsigned char*)malloc(TAG_DEFAULT);
if(!tag){
cerr << " Error in malloc for tag " << endl;
free(header);
free(iv);
return -1;
}
// Open header
memcpy((void*)&ct_len, header, sizeof(uint32_t));
ct_len = ntohl(ct_len);
memcpy(iv, header+sizeof(uint32_t), IV_DEFAULT);
memcpy(tag, header+sizeof(uint32_t)+IV_DEFAULT, TAG_DEFAULT);
unsigned char* aad = (unsigned char*)malloc(sizeof(uint32_t));
if(!aad){
cerr << " Error in aad malloc " << endl;
free(ciphertext);
free(header);
free(tag);
free(iv);
return -1;
}
memcpy(aad, header, sizeof(uint32_t));
if(session_key_clientToClient==NULL){
cerr << " Null key " << endl;
free(ciphertext);
free(header);
free(tag);
free(iv);
free(aad);
return -1;
}
unsigned char* toDecrypt = (unsigned char*)malloc(ct_len);
if(!aad){
cerr << " Error in toDecrypt malloc " << endl;
free(aad);
free(ciphertext);
free(header);
free(tag);
free(iv);
return -1;
}
memcpy(toDecrypt, ciphertext+read, ct_len);
pt_len = auth_enc_decrypt(toDecrypt, ct_len, aad, sizeof(uint32_t), session_key_clientToClient, tag, iv, plaintext);
if(pt_len == 0 || pt_len!=ct_len){
cerr << " Error during decryption " << endl;
free(ciphertext);
free(*plaintext);
free(header);
free(tag);
free(iv);
return -1;
}
free(ciphertext);
free(header);
free(tag);
free(iv);
// check seq number
uint32_t sequence_number = ntohl(*(uint32_t*) (*plaintext));
if(sequence_number<receive_counter_client_client){
cerr << " Error: wrong seq number " << endl;
safe_free(*plaintext,pt_len);
return -1;
}
if(sequence_number==MAX_SEQ_NUM){
cerr << " Error: maximum number of message in the session reached " << endl;
safe_free(*plaintext,pt_len);
return -1;
}
receive_counter_client_client=sequence_number+1;
uint32_t msg_len = pt_len - sizeof(uint32_t);
unsigned char* risp = (unsigned char*)malloc(msg_len);
if(!risp)
return -1;
memcpy(risp, ((*plaintext)+sizeof(uint32_t)), msg_len);
safe_free((*plaintext), pt_len);
*plaintext = risp;
return msg_len;
}
/**
* @brief Receive in a secure way the messages sent by the server, decipher it and return the plaintext in the correspodent parameter. It
* also control the sequence number
*
* @param socket socket id
* @param plaintext plaintext obtained by the decryption of the ciphertext
* @return int plaintext length or -1 if error
*/
int recv_secure(int socket, unsigned char** plaintext)
{
if(sock_id<0)
return -1;
uint32_t header_len = sizeof(uint32_t)+IV_DEFAULT+TAG_DEFAULT;
uint32_t ct_len;
unsigned char* ciphertext = NULL;
uint32_t pt_len;
int ret;
unsigned char* header = (unsigned char*)malloc(header_len);
if(!header){
cerr << " Error in malloc for header " << endl;
return -1;
}
unsigned char* iv = (unsigned char*)malloc(IV_DEFAULT);
if(!iv){
cerr << " Error in malloc for iv " << endl;
free(header);
return -1;
}
unsigned char* tag = (unsigned char*)malloc(TAG_DEFAULT);
if(!tag){
cerr << " Error in malloc for tag " << endl;
free(header);
free(iv);
return -1;
}
// Receive Header
ret = recv(sock_id, (void*)header, header_len, 0);
if(ret <= 0 || ret != header_len){
cerr << " Error in header reception " << ret << endl;
BIO_dump_fp(stdout, (const char*)header, header_len);
free(header);
free(tag);
free(iv);
return -1;
}
// Open header
memcpy((void*)&ct_len, header, sizeof(uint32_t));
memcpy(iv, header+sizeof(uint32_t), IV_DEFAULT);
memcpy(tag, header+sizeof(uint32_t)+IV_DEFAULT, TAG_DEFAULT);
unsigned char* aad = (unsigned char*)malloc(sizeof(uint32_t));
if(!aad){
cerr << " Error in aad malloc " << endl;
free(ciphertext);
free(header);
free(tag);
free(iv);
return -1;
}
memcpy(aad, header, sizeof(uint32_t));
// Receive ciphertext
ct_len = ntohl(ct_len);
ciphertext = (unsigned char*)malloc(ct_len);
if(!ciphertext){
cerr << " Error in malloc for ciphertext " << endl;
free(header);
free(tag);
free(iv);
return -1;
}
ret = recv(sock_id, (void*)ciphertext, ct_len, 0);
if(ret <= 0){
cerr << " Error in AAD reception " << endl;
free(ciphertext);
free(header);
free(tag);
free(iv);
return -1;
}
// Decryption
pt_len = auth_enc_decrypt(ciphertext, ct_len, aad, sizeof(uint32_t), session_key_clientToServer, tag, iv, plaintext);
if(pt_len == 0 || pt_len!=ct_len){
cerr << " Error during decryption " << endl;
free(ciphertext);
free(*plaintext);
free(header);
free(tag);
free(iv);
return -1;
}
free(ciphertext);
free(header);
free(tag);
free(iv);
// check seq number
uint32_t sequece_number = ntohl(*(uint32_t*) (*plaintext));
if(sequece_number<receive_counter){
cerr << " Error: wrong seq number " << endl;
free(plaintext);
return -1;
}
if(sequece_number==MAX_SEQ_NUM){
cerr << " Error: maximum number of message in the session reached " << endl;
safe_free(*plaintext,pt_len);
return -1;
}
receive_counter=sequece_number+1;
return pt_len;
}
/**
* @brief Prepare the message for the client. The plaintext is safely free inside the function
*
* @param plaintext
* @param pt_len
* @param msg_to_send
* @return The length of msg_to_send, 0 if error(s)
*/
int prepare_msg_for_client(unsigned char* pt, uint32_t pt_len, unsigned char** msg_to_send)
{
if(pt==NULL)
return -1;
int ret;
uchar *tag, *iv, *ct, *aad;
uint aad_len;
uint32_t header_len = sizeof(uint32_t)+IV_DEFAULT+TAG_DEFAULT;
// adding sequence number
uint32_t counter_n=htonl(send_counter_client_client);
if(pt_len>UINT32_MAX-sizeof(uint32_t)){
cerr << " Too big number for malloc " << endl;
return -1;
}
uchar* pt_seq = (uchar*)malloc(pt_len+sizeof(uint32_t));
if(!pt_seq){
safe_free(pt, pt_len);
return 0;
}
memcpy(pt_seq, &counter_n, sizeof(uint32_t));
memcpy(pt_seq+sizeof(uint32_t), pt, pt_len);
pt=pt_seq;
pt_len+=sizeof(uint32_t);
int aad_ct_len_net = htonl(pt_len); //Since we use GCM ciphertext == plaintext
if(session_key_clientToClient==NULL){
cerr << " Null key " << endl;
return 0;
}
uint ct_len = auth_enc_encrypt(pt, pt_len, (uchar*)&aad_ct_len_net, sizeof(uint), session_key_clientToClient, &tag, &iv, &ct);
if(ct_len == 0){
cerr << "auth_enc_encrypt failed" << endl;
safe_free(pt, pt_len);
free(iv);
free(tag);
free(ct);
free(pt_seq);
return 0;
}
if(ct_len > UINT_MAX - header_len){
cerr << " Integer overflow " << endl;
safe_free(pt, pt_len);
free(iv);
free(tag);
free(ct);
free(pt_seq);
return 0;
}
uint msg_to_send_len = ct_len + header_len;
uint bytes_copied = 0;
*msg_to_send = (uchar*)malloc(msg_to_send_len);
if(!(*msg_to_send)){
errorHandler(MALLOC_ERR);
safe_free(pt, pt_len);
free(iv);
free(tag);
free(ct);
free(pt_seq);
return 0;
}
memcpy((*msg_to_send) + bytes_copied, &aad_ct_len_net, sizeof(uint32_t));
bytes_copied += sizeof(uint32_t);
memcpy((*msg_to_send) + bytes_copied, iv, IV_DEFAULT);
bytes_copied += IV_DEFAULT;
memcpy((*msg_to_send) + bytes_copied, tag, TAG_DEFAULT);
bytes_copied += TAG_DEFAULT;
memcpy((*msg_to_send) + bytes_copied, ct, ct_len);
bytes_copied += ct_len;
if(bytes_copied!=msg_to_send_len)
cerr << " Warning " << bytes_copied << " != " << msg_to_send_len << endl;
safe_free(pt, pt_len);
free(iv);
free(tag);
free(ct);
if(send_counter_client_client==UINT32_MAX)
return 0;
send_counter_client_client++;
return bytes_copied;
}
/**
* @brief Perform an authenticated encryption and then a send operation - add also the sequence number at the head of the plaintext
*
* @param comm_socket_id socket id
* @param pt buffer to encrypt and send
* @param pt_len len of the buffer
* @return 0 in case of error, 1 otherwise
*/
int send_secure(int comm_socket_id, uchar* pt, int pt_len){
if(comm_socket_id<0)
return 0;
if(pt==NULL)
return 0;
int ret;
uchar *tag, *iv, *ct, *aad;
uint aad_len;
uint32_t header_len = sizeof(uint32_t)+IV_DEFAULT+TAG_DEFAULT;
// adding sequence number
uint32_t counter_n=htonl(send_counter);
uchar* pt_seq = (uchar*)malloc(pt_len+sizeof(uint32_t));
if(!pt_seq){
safe_free(pt, pt_len);
return 0;
}
memcpy(pt_seq , &counter_n, sizeof(uint32_t));
memcpy(pt_seq+ sizeof(uint32_t), pt, pt_len);
pt=pt_seq;
pt_len+=sizeof(uint32_t);
int aad_ct_len_net = htonl(pt_len); //Since we use GCM ciphertext == plaintext
if(session_key_clientToServer==NULL){
cerr << " Null key " << endl;
return 0;
}
uint ct_len = auth_enc_encrypt(pt, pt_len, (uchar*)&aad_ct_len_net, sizeof(uint), session_key_clientToServer, &tag, &iv, &ct);
if(ct_len == 0){
cerr << "auth_enc_encrypt failed" << endl;
free(iv);
free(tag);
free(ct);
return 0;
}
if(ct_len > UINT_MAX - header_len){
cerr << " Integer overflow " << endl;
safe_free(pt, pt_len);
free(iv);
free(tag);
free(ct);
return 0;
}
uint msg_to_send_len = ct_len + header_len;
uint bytes_copied = 0;
uchar* msg_to_send = (uchar*)malloc(msg_to_send_len);
if(!msg_to_send){
errorHandler(MALLOC_ERR);
free(iv);
free(tag);
free(ct);
safe_free(pt, pt_len);
return 0;
}
memcpy(msg_to_send + bytes_copied, &aad_ct_len_net, sizeof(uint));
bytes_copied += sizeof(uint);
memcpy(msg_to_send + bytes_copied, iv, IV_DEFAULT);
bytes_copied += IV_DEFAULT;
memcpy(msg_to_send + bytes_copied, tag, TAG_DEFAULT);
bytes_copied += TAG_DEFAULT;
memcpy(msg_to_send + bytes_copied, ct, ct_len);
bytes_copied += ct_len;
safe_free(pt, pt_len);
ret = send(comm_socket_id, msg_to_send, msg_to_send_len, 0);
if(ret <= 0 || ret != msg_to_send_len){
errorHandler(SEND_ERR);
free(iv);
free(tag);
free(ct);
safe_free(msg_to_send, msg_to_send_len);
return 0;
}
if(send_counter==UINT32_MAX){
errorHandler(SEND_ERR);
free(iv);
free(tag);
free(ct);
safe_free(msg_to_send, msg_to_send_len);
return 0;
}
send_counter++;
safe_free(msg_to_send, msg_to_send_len);
free(iv);
free(tag);
free(ct);
return 1;
}
/**
* @brief It is in charge of handlig the sending of a command to the server
* @param sock_id socket id
* @param cmdToSend data structure which represent the message to send
* @return -1 in case of error
* */
int send_command_to_server(int sock_id, commandMSG* cmdToSend)
{
if(sock_id<0)
return -1;
if(cmdToSend==NULL)
return -1;
uint32_t net_id;
unsigned char* pt = NULL;
uint32_t pt_len = (cmdToSend->opcode==CHAT_CMD || cmdToSend->opcode==STOP_CHAT)? sizeof(uint8_t)+sizeof(uint32_t) : sizeof(uint8_t);
pt = (unsigned char*)malloc(pt_len);
if(!pt)
return -1;
memcpy(pt, &(cmdToSend->opcode), sizeof(uint8_t));
int stop=0;
if(cmdToSend->opcode==CHAT_CMD || cmdToSend->opcode==STOP_CHAT) {
if(cmdToSend->opcode==STOP_CHAT){
cmdToSend->userId = peer_id;
stop=1;
}
net_id = htonl(cmdToSend->userId);
memcpy(pt+sizeof(uint8_t), &net_id, sizeof(uint32_t));
}
int ret = send_secure(sock_id, pt, pt_len);
if(ret==0){
safe_free(pt, pt_len);
return -1;
}
safe_free(pt, pt_len);
if(stop){
send_counter_client_client=0;
receive_counter_client_client=0;
}
return 0;
}
/**
* @brief It send the message to the server
*
* @param sock_id socket id
* @param msgToSend data structure that contains the info for the message
* @return -1 in case of error, 0 otherwise
*/
int send_message(int sock_id, genericMSG* msgToSend)
{
if(sock_id<0)
return -1;
if(msgToSend==NULL)
return -1;
unsigned char* msgInternalPart = NULL; // nonce for client + msg for client
uint32_t msgInternalPart_len = prepare_msg_for_client(msgToSend->payload, msgToSend->length, &msgInternalPart);
if(msgInternalPart_len==0)
return -1;
if(msgInternalPart_len>UINT32_MAX-(sizeof(uint8_t)+sizeof(uint32_t))){
cerr << " Integer Overflow " << endl;
return -1;
}
uint32_t msg_len = msgInternalPart_len+sizeof(uint8_t)+sizeof(uint32_t);
unsigned char* msg = (unsigned char*)malloc(msg_len);
if(!msg){
safe_free(msgInternalPart, msgInternalPart_len);
return -1;
}
int bytes_allocated = 0;
uint32_t net_peer_user_id = htonl(peer_id);
memcpy((void*)msg, &(msgToSend->opcode), sizeof(uint8_t));
bytes_allocated += sizeof(uint8_t);
memcpy((void*)(msg+bytes_allocated), &(net_peer_user_id), sizeof(uint32_t));
bytes_allocated += sizeof(uint32_t);
memcpy((void*)(msg+bytes_allocated), msgInternalPart, msgInternalPart_len);
bytes_allocated += msgInternalPart_len;
if(bytes_allocated!=msg_len)
cout << " WARNING - Something is going wrong " << endl;
int ret = send_secure(sock_id, msg, msg_len);
if(ret==0){
cerr << " send secure failed " << endl;
safe_free(msgInternalPart, msgInternalPart_len);
safe_free(msg, msg_len);
return -1;
}
// safe_free(msgToSend->payload, msgToSend->length); not needed because free inside prepare_msg_for_client
safe_free(msgInternalPart, msgInternalPart_len);
safe_free(msg, msg_len);
return 0;
}
/**
* @brief Receive a message sent by the other communication party and forwarded by the server
*
* @param sock_id socket id
* @param msg string where the received message is inserted
* @return int -1 id error, 0 otherwise
*/
int receive_message(int sock_id, string& msg, unsigned char* msgReceived, uint32_t msgReceived_len)
{
if(sock_id<0)
return -1;
if(msgReceived==NULL)
return -1;
unsigned char* pt = NULL;
uint32_t pt_len = open_msg_by_client(msgReceived, msgReceived_len, &pt);
if(pt_len<=0){
return -1;
}
msg = (string)((char*)pt);
return 0;
}
/**
* @brief Called after authentication it is in charge of receving the user id of the logged user
*
* @param socket
* @return int -1 in case of error, 0 otherwise
*/
int retrieve_my_userID(int socket)
{
if(sock_id<0)
return -1;
unsigned char* plaintext = NULL;
int pt_len = recv_secure(sock_id, &plaintext);
if(pt_len==-1)
return -1;
// check opcode
uint8_t opcode_rec;
memcpy(&opcode_rec, plaintext+4, sizeof(uint8_t));
if(opcode_rec!=USRID){
cerr << " Error: wrong opcode " << endl;
free(plaintext);
return -1;
}
int loggedUser_id_net;
memcpy(&loggedUser_id_net, plaintext+sizeof(uint32_t)+1, sizeof(uint32_t));
loggedUser_id = ntohl(loggedUser_id_net);
return 0;
}
/**
* @brief Send a negative response to the server after a chat request
*
* @param sock_id socket id
* @param refused_user id of the user rejected, it must be in network order
* @return -1 in case of error, 0 otherwise
*/
int automatic_neg_response(int sock_id, int refused_user)
{
if(sock_id<0)
return -1;
if(ntohl(refused_user)<0 || ntohl(refused_user)>REGISTERED_USERS)
return -1;
uint32_t risp_buff_size = sizeof(uint8_t)+sizeof(int);
unsigned char* risp_buff = (unsigned char*)malloc(risp_buff_size);
if(!risp_buff)
return -1;
uint8_t response = CHAT_NEG;
memcpy(risp_buff, (void*)&response, sizeof(uint8_t));
memcpy(risp_buff+1, (void*)&refused_user, sizeof(int));
int ret = send_secure(sock_id, risp_buff, risp_buff_size);
if(ret==-1){
free(risp_buff);
return -1;
}
free(risp_buff);
return 0;
}
/**
* @brief It performs the authentication procedure with the server or the client depending by the passed parameter
*
* @param sock_id socket id
* @param ver AUTH_CLNT_SRV (if authentication between client and server) or AUTH_CLNT_CLNT (if authentication between client and client)
* @return -1 if error, 0 otherwise
*/
int authentication(int sock_id, uint8_t ver)
{
if(sock_id<0)
return -1;
// If the authentication is done with another client with the word "server" indicates the other client
if(ver!=AUTH_CLNT_CLNT && ver!=AUTH_CLNT_SRV)