-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
767 lines (499 loc) · 22.2 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
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
#include "opts.h"
#include "attrib.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <assert.h>
#include <stdarg.h>
#include <netdb.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <stdarg.h>
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <malloc.h>
#include <bits/string3.h>
FILE *logfile;
#define serverQlen 15
#define MAXBYTE 12000
#define HOME "/home/lasitha/"
#define CERTF HOME "cert.pem"
#define KEYF HOME "key.pem"
#define CHK_NULL(x) if((x) == NULL) exit(1);
#define CHK_ERR(err, s) if((err) == -1) { perror(s); exit(1); }
#define CHK_SSL(err) if((err) == -1) { ERR_print_errors_fp(stderr); exit(2); }
char *server_addr = "192.168.2.20"; //"0.0.0.0";
int server_port = 12443;
int client_port = 443;
int server_socket;
struct sockaddr_in server_sa;
unsigned int server_sa_len;
int getHeaderValue(char *header, char *buf, char *value) {
char *tmp = (char*) calloc(strlen(buf), sizeof (char));
bcopy(buf, tmp, strlen(buf));
char* line;
while ((line = strtok_r(tmp, "\r\n", &tmp))) {
if (strstr(line, header)) {
strtok_r(NULL, " ", &line);
strcpy(value, line);
return 1;
}
}
return 0;
}
int server_init(char *addr, int port, int maxconn) {
struct sockaddr_in server;
long ipaddr;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("socket()");
exit(1);
}
server.sin_family = AF_INET;
inet_pton(AF_INET, addr, &ipaddr);
server.sin_addr.s_addr = ipaddr;
// server.sin_addr.s_addr=htons(INADDR_ANY);
server.sin_port = htons(port);
if (bind(server_socket, (struct sockaddr *) &server, sizeof (server)) < 0) {
perror("bind()");
exit(1);
}
listen(server_socket, maxconn);
//fcntl(server_socket, F_SETFL, O_NONBLOCK);
return server_socket;
}
int CreateCsocket(struct hostent *host) {
int sd;
struct sockaddr_in Caddr;
sd = socket(AF_INET, SOCK_STREAM, 0);
memset(&Caddr, 0, sizeof (Caddr));
Caddr.sin_family = AF_INET;
Caddr.sin_port = htons(client_port);
Caddr.sin_addr.s_addr = *(long*) (host ->h_addr);
if (connect(sd, (struct sockaddr*) &Caddr, sizeof (Caddr)) == -1) {
printf("Cannot connect to Server");
}
return sd;
}
typedef struct pxy_conn_ctx {
unsigned int clienthello_search : 1; /* 1 if waiting for hello */
unsigned int clienthello_found : 1; /* 1 if conn upgrade to SSL */
/* server name indicated by client in SNI TLS extension */
char *sni;
/* original certificate */
X509 *origcrt;
opts_t *opts;
} pxy_conn_ctx_t;
static void pxy_sslctx_setoptions(SSL_CTX *sslctx, pxy_conn_ctx_t *ctx) {
SSL_CTX_set_options(sslctx, SSL_OP_ALL);
#ifdef SSL_OP_TLS_ROLLBACK_BUG
SSL_CTX_set_options(sslctx, SSL_OP_TLS_ROLLBACK_BUG);
#endif /* SSL_OP_TLS_ROLLBACK_BUG */
#ifdef SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
SSL_CTX_set_options(sslctx, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
#endif /* SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION */
#ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
SSL_CTX_set_options(sslctx, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
#endif /* SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS */
#ifdef SSL_OP_NO_TICKET
SSL_CTX_set_options(sslctx, SSL_OP_NO_TICKET);
#endif /* SSL_OP_NO_TICKET */
SSL_CTX_set_cipher_list(sslctx, ctx->opts->ciphers);
}
static SSL_CTX *
pxy_srcsslctx_create(pxy_conn_ctx_t *ctx, X509 *crt, STACK_OF(X509) * chain,
EVP_PKEY *key) {
SSL_CTX *sslctx = SSL_CTX_new(ctx->opts->sslmethod);
if (!sslctx)
return NULL;
pxy_sslctx_setoptions(sslctx, ctx);
SSL_CTX_set_session_cache_mode(sslctx, SSL_SESS_CACHE_SERVER |
SSL_SESS_CACHE_NO_INTERNAL);
#ifdef USE_SSL_SESSION_ID_CONTEXT
SSL_CTX_set_session_id_context(sslctx, (void *) (&ssl_session_context),
sizeof (ssl_session_context));
#endif /* USE_SSL_SESSION_ID_CONTEXT */
#ifndef OPENSSL_NO_DH
if (ctx->opts->dh) {
SSL_CTX_set_tmp_dh(sslctx, ctx->opts->dh);
} else {
SSL_CTX_set_tmp_dh_callback(sslctx, ssl_tmp_dh_callback);
}
#endif /* !OPENSSL_NO_DH */
#ifndef OPENSSL_NO_ECDH
if (ctx->opts->ecdhcurve) {
EC_KEY *ecdh = ssl_ec_by_name(ctx->opts->ecdhcurve);
SSL_CTX_set_tmp_ecdh(sslctx, ecdh);
EC_KEY_free(ecdh);
} else {
EC_KEY *ecdh = ssl_ec_by_name(NULL);
SSL_CTX_set_tmp_ecdh(sslctx, ecdh);
EC_KEY_free(ecdh);
}
#endif /* !OPENSSL_NO_ECDH */
SSL_CTX_use_certificate(sslctx, crt);
SSL_CTX_use_PrivateKey(sslctx, key);
for (int i = 0; i < sk_X509_num(chain); i++) {
X509 *c = sk_X509_value(chain, i);
//ssl_x509_refcount_inc(c); /* next call consumes a reference */
SSL_CTX_add_extra_chain_cert(sslctx, c);
}
return sslctx;
}
static cert_t *
pxy_srccert_create(pxy_conn_ctx_t *ctx) {
cert_t *cert = NULL;
cert = cert_new();
cert->crt = ssl_x509_forge(ctx->opts->cacrt, ctx->opts->cakey, ctx->origcrt, ctx->opts->key, NULL, ctx->opts->crlurl);
cert_set_key(cert, ctx->opts->key);
cert_set_chain(cert, ctx->opts->chain);
return cert;
}
static SSL *pxy_srcssl_create(pxy_conn_ctx_t *ctx, SSL *origssl) {
cert_t *cert;
ctx->origcrt = SSL_get_peer_certificate(origssl);
cert = pxy_srccert_create(ctx);
if (!cert)
return NULL;
SSL_CTX *sslctx = pxy_srcsslctx_create(ctx, cert->crt, cert->chain,
cert->key);
cert_free(cert);
SSL *ssl = SSL_new(sslctx);
SSL_CTX_free(sslctx); /* SSL_new() increments refcount */
if (!ssl) {
return NULL;
}
#ifdef SSL_MODE_RELEASE_BUFFERS
/* lower memory footprint for idle connections */
SSL_set_mode(ssl, SSL_get_mode(ssl) | SSL_MODE_RELEASE_BUFFERS);
#endif /* SSL_MODE_RELEASE_BUFFERS */
return ssl;
}
static SSL *
pxy_dstssl_create(pxy_conn_ctx_t *ctx) {
SSL_CTX *sslctx;
SSL *ssl;
sslctx = SSL_CTX_new(SSLv23_client_method());
if (!sslctx) {
return NULL;
}
SSL_CTX_set_verify(sslctx, SSL_VERIFY_NONE, NULL);
ssl = SSL_new(sslctx);
SSL_CTX_free(sslctx); /* SSL_new() increments refcount */
if (!ssl) {
return NULL;
}
#ifndef OPENSSL_NO_TLSEXT
if (ctx->sni) {
SSL_set_tlsext_host_name(ssl, ctx->sni);
}
#endif /* !OPENSSL_NO_TLSEXT */
#ifdef SSL_MODE_RELEASE_BUFFERS
/* lower memory footprint for idle connections */
SSL_set_mode(ssl, SSL_get_mode(ssl) | SSL_MODE_RELEASE_BUFFERS);
#endif /* SSL_MODE_RELEASE_BUFFERS */
return ssl;
}
int main(int argc, char **argv) {
int pid;
opts_t *opts;
opts = opts_new();
EVP_PKEY_free(opts->cakey);
opts->cakey = ssl_key_load(KEYF);
opts->cacrt = ssl_x509_load(CERTF);
//ssl_x509_refcount_inc(opts->cacrt);
sk_X509_insert(opts->chain, opts->cacrt, 0);
if (opts->cakey && opts->cacrt &&
(X509_check_private_key(opts->cacrt, opts->cakey) != 1)) {
printf("CA cert does not match key.\n");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
opts->ciphers = strdup(DFLT_CIPHERS);
opts->key = ssl_key_genrsa(1024);
opts->sslmethod = SSLv23_server_method();
pxy_conn_ctx_t *ctx = malloc(sizeof (pxy_conn_ctx_t));
memset(ctx, 0, sizeof (pxy_conn_ctx_t));
ctx->opts = opts;
logfile = fopen("log.bin", "w");
SSL_CTX *Cctx;
SSL *Cssl;
SSL_CTX *Sctx;
SSL *Sssl;
signal(SIGPIPE, SIG_IGN);
struct timeval tv;
tv.tv_sec = 2; // 2 seconds
tv.tv_usec = 0;
//server_init(server_addr, server_port, serverQlen);
char *addr = server_addr;
int port = server_port;
int maxconn = serverQlen;
struct sockaddr_in server;
long ipaddr;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("socket()");
exit(1);
}
server.sin_family = AF_INET;
inet_pton(AF_INET, addr, &ipaddr);
server.sin_addr.s_addr = ipaddr;
// server.sin_addr.s_addr=htons(INADDR_ANY);
server.sin_port = htons(port);
if (bind(server_socket, (struct sockaddr *) &server, sizeof (server)) < 0) {
perror("bind()");
exit(1);
}
listen(server_socket, maxconn);
SSL_load_error_strings();
SSLeay_add_ssl_algorithms();
while (1) {
char buffer[8192];
pxy_conn_ctx_t *Cctx = malloc(sizeof (pxy_conn_ctx_t));
int client = accept(server_socket, (struct sockaddr*) &server_sa, &server_sa_len);
unsigned char buf[1024];
ssize_t n;
const unsigned char *chello;
int rv;
n = recv(client, buf, sizeof (buf), MSG_PEEK); // Take hello massages containing data from client and store in buff.
if (n == -1) {
printf("Error peeking on fd, aborting "
"connection\n");
//evutil_closesocket(fd);
//pxy_conn_ctx_free(ctx, 1);
return;
}
if (n == 0) {
/* socket got closed while we were waiting */
//evutil_closesocket(fd);
//pxy_conn_ctx_free(ctx, 1);
return;
}
rv = ssl_tls_clienthello_parse(buf, n, 0, &chello, &Cctx->sni); // Identify hello massage and append to chello field.
if ((rv == 1) && !chello) {
printf("Peeking did not yield a (truncated) "
"ClientHello message, "
"aborting connection\n");
//evutil_closesocket(fd);
//pxy_conn_ctx_free(ctx, 1);
return;
}
/* pid = fork();
if (pid != 0) {
//wait(NULL);
signal(SIGCHLD, SIG_IGN);
close(client);
} else {
*/
if (client > 0) {
//close(server_socket);
printf("################################## Client Started #################################\n\n");
char clientip[20];
struct sockaddr_in addr;
bzero((char *) &addr, sizeof (addr));
addr.sin_family = AF_INET;
socklen_t addr_sr = sizeof (addr);
getsockopt(client, SOL_IP, 80, &addr, &addr_sr);
printf("Original Dest IP address is: %s\n", inet_ntoa(addr.sin_addr));
setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, (const char*) &tv, sizeof tv);
setsockopt(client, SOL_SOCKET, SO_SNDTIMEO, (const char*) &tv, sizeof tv);
int clientsock;
struct sockaddr_in Caddr;
clientsock = socket(AF_INET, SOCK_STREAM, 0);
memset(&Caddr, 0x00, sizeof (Caddr));
Caddr.sin_family = AF_INET;
Caddr.sin_port = htons(client_port);
Caddr.sin_addr.s_addr = addr.sin_addr.s_addr; // *(long*) (inet_ntoa(addr.sin_addr));
if (connect(clientsock, (struct sockaddr*) &Caddr, sizeof (Caddr)) == -1) {
printf("Cannot connect to Server");
}
if (clientsock) {
//SSL_CTX *Cctx;
SSL *Cssl = pxy_dstssl_create(Cctx);
// Cctx = SSL_CTX_new(SSLv23_client_method());
//Cssl = SSL_new(Cctx);
SSL_set_fd(Cssl, clientsock);
if (SSL_connect(Cssl) > 0) {
Sssl = malloc(sizeof (SSL));
Sssl = pxy_srcssl_create(ctx, Cssl);
SSL_set_fd(Sssl, client);
SSL_shutdown(Cssl);
close(clientsock);
SSL_free(Cssl);
/// SSL_CTX_free(Cctx);
if (SSL_accept(Sssl) > 0) {
printf("%s\n\n", "SSL Handshake Complete.........................................................\n\n");
// char buffer[MAXBYTE * sizeof (char)];//(char*) malloc(MAXBYTE * sizeof (char));
memset(buffer, '\0', sizeof (buffer));
int bytes = 0;
int line_length = 0;
int i = 0;
int contlen = -1;
int bodylen = 0;
bytes = SSL_read(Sssl, buffer, 1);
bytes += SSL_read(Sssl, buffer + 1, sizeof (buffer) - 1);
buffer[bytes] = 0x00;
if (bytes > 0 || SSL_pending(Sssl) > 0) {
while (i <= bytes) {
if (buffer[i] == '\n') {
if (line_length == 0) {
i++;
break;
} else line_length = 0;
} else if (buffer[i] != '\r') line_length++;
i++;
}
bodylen = -i;
bodylen += bytes;
// printf("%s\n\n", "Client Request.........................................................\n\n");
// struct hostent* host = gethostbyname(line);
if (addr.sin_addr.s_addr != NULL) {
printf("%s\n\n", "DNS Success .........................................................\n\n");
int clientsock1;
struct sockaddr_in Caddr;
clientsock1 = socket(AF_INET, SOCK_STREAM, 0);
memset(&Caddr, 0x00, sizeof (Caddr));
Caddr.sin_family = AF_INET;
Caddr.sin_port = htons(client_port);
Caddr.sin_addr.s_addr = addr.sin_addr.s_addr; // *(long*) (inet_ntoa(addr.sin_addr));
if (connect(clientsock1, (struct sockaddr*) &Caddr, sizeof (Caddr)) == -1) {
printf("Cannot connect to Server");
}
// = CreateCsocket(host);
if (clientsock1) {
Cctx = SSL_CTX_new(SSLv23_client_method());
Cssl = SSL_new(Cctx);
SSL_set_fd(Cssl, clientsock1);
if (SSL_connect(Cssl) > 0) {
int len;
len = strlen(buffer);
setsockopt(clientsock1, SOL_SOCKET, SO_RCVTIMEO, (const char*) &tv, sizeof tv);
setsockopt(clientsock1, SOL_SOCKET, SO_SNDTIMEO, (const char*) &tv, sizeof tv);
char value[15];
memset(value, '\0', sizeof (value));
SSL_write(Cssl, buffer, bytes);
if (getHeaderValue("Content-Length:", buffer, &value)) {
int contlen = atoi(value);
while (contlen > bodylen) {
memset(buffer, '\0', sizeof (buffer));
bytes = SSL_read(Sssl, buffer, sizeof (buffer) - 1);
if (bytes <= 0) break;
bodylen += bytes;
SSL_write(Cssl, buffer, bytes);
bytes = 0;
}
}
if (len) {
memset(buffer, '\0', sizeof (buffer));
contlen = -1;
bodylen = 0;
bytes = 0;
bytes = SSL_read(Cssl, buffer, sizeof (buffer) - 1);
printf("Server Responded ..................%d......................\n\n", bytes);
if (bytes > 0) {
//fprintf(logfile , "Server Response : \n\n");
//fwrite("Server Response : \n\n",strlen("Server Response : \n\n"),10,logfile);
memset(value, '\0', sizeof (value));
if (getHeaderValue("Content-Length:", buffer, value)) {
contlen = atoi(value);
i = 0;
line_length = 0;
while (i <= bytes) {
if (buffer[i] == '\n') {
if (line_length == 0) {
i++;
break;
} else line_length = 0;
} else if (buffer[i] != '\r') line_length++;
i++;
}
bodylen = -i;
printf("\n$$$$$$$$$$$$$ header length : %d $$$$$$$$$$$$$\n\n", i);
printf("$$$$$$$$$$$$$ ContLen %d $$$$$$$$$$$$$\n\n", contlen);
}
char trnsfcoding[10];
memset(trnsfcoding, '\0', sizeof (trnsfcoding));
getHeaderValue("Tranfer-Coding", buffer, &trnsfcoding);
while (bytes > 0) {
bodylen += bytes;
//
//fwrite(buffer,bytes,10,logfile);
//fprintf(logfile , "%s",buffer);
//
int iswrite = SSL_write(Sssl, buffer, bytes);
//printf("content : %s\n\n", buffer);
if (iswrite == -1) {
printf("write fail to client");
break;
}
if (contlen > -1) {
if (contlen <= bodylen) {
break;
}
} else if (trnsfcoding == NULL || trnsfcoding != "chunked") {
if (strstr(buffer, "0\r\n\r\n") != NULL) {
break;
}
}
memset(buffer, '\0', sizeof (buffer) * sizeof (char));
bytes = 0;
bytes = SSL_read(Cssl, buffer, sizeof (buffer) - 1);
}
}
printf("Server Responded body ..................%i......................\n\n", bodylen);
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Request Complete @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n");
//fprintf(logfile , );
//fwrite("End Server Response : \n\n\n\n\n",strlen("End Server Response : \n\n\n\n\n"),10,logfile);
} else {
printf("Failed to write to server.....");
}
} else {
printf("connection fail to server");
}
} else {
printf("Create Server connection Failed.....");
}
} else {
printf("DNS Failed.....");
}
} else {
printf("Failed to read client Request.....");
}
} else {
printf("Error ssl handshake.\n");
}
printf("################################## Client Closed #################################\n\n");
close(client);
SSL_free(Sssl);
SSL_CTX_free(Sctx);
} else {
SSL_shutdown(Cssl);
close(clientsock);
SSL_free(Cssl);
SSL_CTX_free(Cctx);
}
}
} else {
//
perror("Unable to accept Client");
}
// exit (0);
//}
}
close(server_socket);
}