forked from KIJ-D-NANA/ChatServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
395 lines (355 loc) · 11.4 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>
#include <openssl/sha.h>
#include "rc4encryption.h"
#include "RSACrypto.h"
#include "Base64.h"
#define KEY_LENGTH 2048
#define PUB_EXP 3
#define PRINT_KEYS
typedef struct Client{
char Name[256];
int sockfd;
char public_key[2048];
RSA* keypair;
struct Client* Next;
struct Client* Previous;
}Client;
Client* head;
Client* tail;
char *public_key;
char *private_key;
void InitRSA();
int CheckHashValidation(size_t input_len, unsigned char* raw, char* hash_value);
void* SomeAwesomeThings(void* Param){
Client* theClient = (Client*)Param;
char* message = (char*)malloc(4086);
char sendMessage[4086];
char encrypt[4086];
char* receiver;
char* tmp;
char* ender;
char* err;
Client* ClientCounter;
int msgSize;
int usernameSet = 0;
int nameLength;
int sub_msg_len;
int encrypt_len;
//RC4 component
RC4Container RC4key;
int iter1;
int iter2;
RC4key.iter1 = &iter1;
RC4key.iter2 = &iter2;
int RC4KeySet = 0;
//
unsigned char hash_out[SHA_DIGEST_LENGTH + 1];
char hash_string[SHA_DIGEST_LENGTH * 2 + 1];
int iterator;
memset(sendMessage, '\0', sizeof(sendMessage));
err = (char*) malloc(130);
// if((msgSize = read(theClient->sockfd, message, sizeof(message) - 1)) > 0){
// message[msgSize] = '\0';
// int nameLength = strstr(message, "\r\n.\r\n") - message;
// for(msgSize = 0; msgSize < nameLength; msgSize++){
// theClient->Name[msgSize] = message[msgSize];
// }
// theClient->Name[nameLength] = '\0';
// printf("%s has connected\n", theClient->Name);
// }
while((msgSize = read(theClient->sockfd, message, 4086 - 1)) > 0){
message[msgSize] = '\0';
sub_msg_len = 0;
while((ender = strstr(message, "\r\n.\r\n")) != NULL){
// *ender = '\0';
sub_msg_len += ender - message + 5;
if(sub_msg_len > msgSize)
break;
// printf("%s\n", message);
//Do things here
tmp = strstr(message, "\r\n");
*tmp = '\0';
printf("%s\n", message);
if(strcmp(message, "Mode: Public") == 0){
tmp = tmp + 2;
nameLength = strstr(tmp, "\r\n.\r\n") - tmp + 5;
sprintf(sendMessage,"%s\r\nUser: %s\r\n", message, theClient->Name);
strncat(sendMessage, tmp, nameLength);
for(ClientCounter = head; ClientCounter != NULL; ClientCounter = ClientCounter->Next){
if(ClientCounter == theClient) continue;
write(ClientCounter->sockfd, sendMessage, sizeof(sendMessage));
}
}
else if((strcmp(message, "Mode: Private") == 0) || (strcmp(message, "Mode: InitPriv") == 0) || (strcmp(message, "Mode: AccPriv") == 0) ){
tmp = tmp + 2;
tmp = strstr(tmp, " ") + 1;
receiver = tmp;
tmp = strstr(tmp, "\r\n");
*tmp = '\0';
tmp = tmp + 2;
nameLength = strstr(tmp, "\r\n.\r\n") - tmp + 5;
sprintf(sendMessage,"%s\r\nUser: %s\r\n", message, theClient->Name);
strncat(sendMessage, tmp, nameLength);
for(ClientCounter = head; ClientCounter != NULL; ClientCounter = ClientCounter->Next){
if(strcmp(receiver, ClientCounter->Name) == 0){
write(ClientCounter->sockfd, sendMessage, sizeof(sendMessage));
break;
}
}
}
else if(strcmp(message, "Mode: GetList") == 0){
sprintf(sendMessage, "Mode: List\r\n");
for(ClientCounter = head; ClientCounter != NULL; ClientCounter = ClientCounter->Next){
if(ClientCounter == theClient)continue;
strcat(sendMessage, ClientCounter->Name);
strcat(sendMessage, "\r\n");
}
strcat(sendMessage, "\r\n.\r\n");
write(theClient->sockfd, sendMessage, sizeof(sendMessage));
}
else if(strcmp(message, "Mode: Username") == 0){
tmp = tmp + 2;
if(usernameSet == 0){
if(RC4KeySet == 1){
printf("Setting username\n");
nameLength = strstr(tmp, "\r\n.\r\n") - tmp;
char* decode;
int decode_len = Base64decode(tmp, (unsigned char**)&decode, nameLength);
encrypt_len = RC4Crypt(decode_len, (unsigned char*)decode, (unsigned char*)encrypt, &RC4key);
free(decode);
tmp = strstr(encrypt, "\r\n.,\r\n");
nameLength = tmp - encrypt;
*tmp = '\0';
tmp = tmp + 6;
if(CheckHashValidation(nameLength, (unsigned char*)encrypt, tmp) == 1){
for(msgSize = 0; msgSize < nameLength; msgSize++){
theClient->Name[msgSize] = *(encrypt + msgSize);
}
theClient->Name[nameLength] = '\0';
printf("%s\n", theClient->Name);
usernameSet++;
}
}
}
}
else if(strcmp(message, "Mode: GetCA") == 0){
sprintf(sendMessage, "Mode: ServCA\r\n");
strcat(sendMessage, public_key);
strcat(sendMessage, "\r\n.\r\n");
write(theClient->sockfd, sendMessage, sizeof(sendMessage));
}
else if(strcmp(message, "Mode: SetPubKey") == 0){
tmp = tmp + 2;
nameLength = strstr(tmp, "\r\n.\r\n") - tmp;
if(RC4KeySet == 1){
char* decoded;
int decoded_len = Base64decode(tmp, (unsigned char**)&decoded, nameLength);
encrypt_len = RC4Crypt(decoded_len, (unsigned char*)decoded, (unsigned char*)encrypt, &RC4key);
free(decoded);
encrypt[encrypt_len] = '\0';
tmp = strstr(encrypt, "\r\n.,\r\n");
nameLength = tmp - encrypt;
*tmp = '\0';
tmp = tmp + 6;
if(CheckHashValidation(nameLength, (unsigned char*)encrypt, tmp) == 1){
for(iterator = 0; iterator < nameLength; iterator++){
*(theClient->public_key + iterator) = *(encrypt + iterator);
}
*(theClient->public_key + nameLength) = '\0';
}
else{
//TODO: Return some error
printf("Failed to set public key\n");
sprintf(sendMessage, "Mode: FailPubKey\r\n.\r\n");
write(theClient->sockfd, sendMessage, sizeof(sendMessage));
}
}
}
else if(strcmp(message, "Mode: SetRC4Key") == 0){
if(RC4KeySet == 0){
tmp = tmp + 2;
nameLength = strstr(tmp, "\r\n.\r\n") - tmp;
char* decoded;
int dedoded_len = Base64decode(tmp, (unsigned char**)&decoded, nameLength);
if((encrypt_len = private_decrypt((unsigned char*)decoded, dedoded_len, (unsigned char*)private_key, (unsigned char*)encrypt, RSA_PKCS1_OAEP_PADDING)) == -1){
ERR_load_crypto_strings();
ERR_error_string(ERR_get_error(), err);
fprintf(stderr, "Error decrypting message: %s\n", err);
sprintf(sendMessage, "Mode: FailRC4Key\r\n.\r\n");
write(theClient->sockfd, sendMessage, sizeof(sendMessage));
}
else{
tmp = strstr(encrypt, "\r\n.,\r\n");
nameLength = tmp - encrypt;
*tmp = '\0';
tmp = tmp + 6;
//TODO: Check if hash of encrypt is the same with tmp
if(CheckHashValidation(nameLength, (unsigned char*)encrypt, tmp) == 1){
initRC4key(&RC4key, encrypt, nameLength);
RC4KeySet = 1;
}
else{
sprintf(sendMessage, "Mode: FailRC4Key\r\n.\r\n");
write(theClient->sockfd, sendMessage, sizeof(sendMessage));
}
}
free(decoded);
}
}
else if(strcmp(message, "Mode: GetPubKey") == 0){
tmp = tmp + 2;
tmp = strstr(tmp, " ") + 1;
receiver = tmp;
tmp = strstr(tmp, "\r\n");
*tmp = '\0';
for(ClientCounter = head; ClientCounter != NULL; ClientCounter = ClientCounter->Next){
if(strcmp(receiver, ClientCounter->Name) == 0)
break;
}
if(ClientCounter != NULL){
if(ClientCounter->public_key[0] != '\0' && RC4KeySet == 1){
SHA1((unsigned char*)ClientCounter->public_key, strlen(ClientCounter->public_key), (unsigned char*)hash_out);
for(iterator = 0; iterator < SHA_DIGEST_LENGTH; iterator++){
sprintf(&hash_string[iterator * 2], "%02x", (unsigned int)hash_out[iterator]);
}
sprintf(sendMessage, "%s\r\n.,\r\n%s", ClientCounter->public_key, hash_string);
encrypt_len = RC4Crypt(strlen(sendMessage), (unsigned char*)sendMessage, (unsigned char*)encrypt, &RC4key);
encrypt[encrypt_len] = '\0';
char* encoded;
int encoded_len = Base64encode(encrypt, encrypt_len, &encoded);
encoded[encoded_len] = '\0';
sprintf(sendMessage, "Mode: ClientPubKey\r\nUser: %s\r\n%s\r\n.\r\n", ClientCounter->Name, encoded);
free(encoded);
printf("%s\n", sendMessage);
printf("%s\n", ClientCounter->public_key);
write(theClient->sockfd, sendMessage, sizeof(sendMessage));
}
else{
sprintf(sendMessage, "Mode: FailPubKey\r\nUser: %s\r\n.\r\n", ClientCounter->Name);
}
}
}
*ender = '\0';
ender += 5;
message = ender;
}
}
printf("%s has been disconnected\n", theClient->Name);
if(theClient == head){
head = theClient->Next;
if(head != NULL)head->Previous = NULL;
else tail = NULL;
}
else{
theClient->Previous->Next = theClient->Next;
if(theClient == tail){
tail = theClient->Previous;
}
}
if(theClient->keypair != NULL)
RSA_free(theClient->keypair);
free(theClient);
return NULL;
}
int main(int argc, char **argv)
{
InitRSA();
int portNum;
int listenfd = 0, connfd = 0;
head = tail = NULL;
if(argc > 2){
portNum = atoi(argv[2]);
}
else{
portNum = 5050;
}
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(listenfd == -1){
printf("Socket retrival failed\n");
return -1;
}
printf("Socket retrival success\n");
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(portNum);
if(bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr)) == -1){
printf("Failed to bind port\n");
return -1;
}
if(listen(listenfd, 10) == -1){
printf("Failed to listen");
return -1;
}
while(1){
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
printf("New user has connected\n");
Client* newClient = (Client*) malloc(sizeof(Client));
newClient->sockfd = connfd;
newClient->public_key[0] = '\0';
strcpy(newClient->Name, "");
newClient->keypair = NULL;
if(head == NULL){
head = newClient;
newClient->Previous = NULL;
}
else{
tail->Next = newClient;
}
newClient->Previous = tail;
tail = newClient;
newClient->Next = NULL;
pthread_t clientThread;
pthread_create(&clientThread, NULL, SomeAwesomeThings, (void*)newClient);
}
/*RSA_free(keypair);*/
free(public_key);
free(private_key);
return 0;
}
void InitRSA(){
//Generating RSA key
size_t file_size;
FILE* private_key_file = fopen("./private.pem", "r");
FILE* public_key_file = fopen("./public.pem", "r");
fseek(private_key_file, 0, SEEK_END);
file_size = ftell(private_key_file);
fseek(private_key_file, 0, SEEK_SET);
private_key = (char*)malloc(file_size + 1);
file_size = fread(private_key, 1, file_size, private_key_file);
fclose(private_key_file);
private_key[file_size] = '\0';
fseek(public_key_file, 0, SEEK_END);
file_size = ftell(public_key_file);
fseek(public_key_file, 0, SEEK_SET);
public_key = (char*)malloc(file_size + 1);
file_size = fread(public_key, 1, file_size, public_key_file);
fclose(public_key_file);
public_key[file_size] = '\0';
printf("%s\n\n%s\n\n", private_key, public_key);
}
int CheckHashValidation(size_t input_len, unsigned char* raw, char* hash_value){
//TODO: Check if hash of encrypt is the same with tmp
char hash_out[20];
char hash_string[SHA_DIGEST_LENGTH * 2 + 1];
SHA1(raw, input_len, (unsigned char*)hash_out);
int i;
int hash_valid;
for(i = 0; i < SHA_DIGEST_LENGTH; i++){
sprintf(&hash_string[i * 2], "%02x", (unsigned int)hash_out[i]);
}
hash_valid = 1;
for(i = 0; i < SHA_DIGEST_LENGTH * 2; i++){
if(hash_string[i] != hash_value[i]){
hash_valid = 0;
break;
}
}
return hash_valid;
}