-
Notifications
You must be signed in to change notification settings - Fork 5
/
uttun.c
1421 lines (1272 loc) · 37.9 KB
/
uttun.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
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
/* Copyright © 2018 Björn Victor (bjorn@victor.se) */
/* Program to tunnel UDP over TCP,
intended for (or as a prototype for) the bridge program for various
Chaosnet implementations.
The main use case is when one end of the UDP communication doesn't
have a public IP address, and thus can't receive UDP at a specific
port, and only in response to an outgoing UDP packet.
However, in this situation the server does't know the client
address beforehand, and it becomes hard/unfeasible to configure a
firewall for protection. Thus we need to use TLS (which is a good
thing anyway). The drawback is the cert management...
*/
// TODO:
// x TLS (with cert verification)
// - statistics
// - keep-alive?
// PoC working, now integrate in cbridge:
// - need more than one TLS listener
// - need to know which TLS conn to send UDP pkts to - cbridge knows
// - make a library of it
// -- TLS reader thread for each defined server link
// -- TLS sender: no thread, just a route thing
// -- TLS keep-open thread
// - skip CHUDP header, just send Chaos pkts with record length
// -- possibly check pkts coming from the right address (trailer) like CHUDP (in cbridge)
// - locks:
// -- per link: reconnect (mutex + cond) + tcp_is_open (mutex + cond)
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef UTTUN_TLS
// **** TLS stuff.
// See https://wiki.openssl.org/index.php/Simple_TLS_Server,
// https://github.com/CloudFundoo/SSL-TLS-clientserver
# define UTTUN_TLS 1
#endif
#ifndef UTTUN_TLS_VERIFY
# define UTTUN_TLS_VERIFY 1
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <pthread.h>
#if UTTUN_TLS
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/asn1.h>
#include <openssl/x509.h>
#endif
// connect from remote/client (non-permanent-IP) end to server (with permanent IP)
// read UDP pkts at one end, send to the other over TCP, resend to UDP dest there
// - so two threads at each end
// when either end gets an error (on TCP), try to re-open TCP,
// - can happen both on reading and writing
// - client connects, server listens
// - so third thread does the connect/listen
// re-parse server spec at re-open time (in case it moves)
// some authentication?
// - minimize attack vector - maybe use TLS?
// - initial handshake to "validate" that the other end speaks BSWM?
// some robustness/input (content) validation?
// - Check for CHUDP header, at least? Minimum length (CHUDP+Chaos headers)?
// consider using setsockopt(tsock, SO_LINGER, {1, 0}) to make close do RST, in error cases
// (cf https://stackoverflow.com/questions/6439790/sending-a-reset-in-tcp-ip-socket-connection)
// add statistics
// - how often do we need to reopen?
// - how often does the address change?
// - how many partial chunks are received?
// maybe add a keep-alive MARK signal, based on how long ago TCP was used?
#define UTTUN_BACKLOG 3
// minimum Chaos max (8*2+288+3*2) + CHUDP header (4) = 510 + UDP header (4*2) = 518
// typical Ether MTU is 1500 - 20 (ip header) - 8 (udp header) = 1472
#define UTTUN_UDP_MAXLEN 1472
#define UTTUN_TCP_MAXLEN UTTUN_UDP_MAXLEN
// default ports
#define UTTUN_UDP_PORT 42042
#define UTTUN_TCP_PORT 42042
int usock; // UDP socket, bidirectional
int tsock; // TCP socket, bidirectional
int tursock; // TCP ur-socket (for listen/accept)
#if UTTUN_TLS
SSL *uttun_ssl; // SSL conn
#if UTTUN_TLS_VERIFY
char tls_key_file[PATH_MAX] = "key.pem";
char tls_cert_file[PATH_MAX] = "cert.pem";
char tls_ca_file[PATH_MAX] = "ca-chain.cert.pem";
#endif
#endif
struct sockaddr_in udpdest; /* where to send pkts read from TCP */
struct sockaddr_in tcpdest; /* where to send pkts read from UDP */
// condition var to wait for open TCP conn
pthread_cond_t tcp_is_open = PTHREAD_COND_INITIALIZER;
// and related mutex
pthread_mutex_t tcp_is_open_mutex = PTHREAD_MUTEX_INITIALIZER;
// condition var to wait for to re-open TCP conn
pthread_cond_t tcp_reconnect = PTHREAD_COND_INITIALIZER;
pthread_mutex_t tcp_reconnect_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_t uthread, tthread, cthread;
// for keeping trace printouts unmixed
pthread_mutex_t trace_print_mutex = PTHREAD_MUTEX_INITIALIZER;
#define TRACE_SIGNALS 1
#define TRACE_UDP 2
#define TRACE_TCP 4
#define TRACE_PACKETS 8
#define TRACE_TLS 16
int trace = 0;
void dumppkt(unsigned char *ucp, int cnt);
void tprintln(char *fmt, ...)
{
struct tm ltime;
char ts[5+3+3+3+3+3+1];
time_t now = time(NULL);
pthread_t self = pthread_self();
char tid[14];
sprintf(tid, "%#08x", (unsigned int)self);
localtime_r(&now,<ime);
strftime(ts, sizeof(ts), "%Y-%d-%m %H:%M:%S", <ime);
pthread_mutex_lock(&trace_print_mutex);
fprintf(stderr, "%s %-10s ", ts, (self == uthread ? "UDP" : (self == tthread ? "TCP" : self == cthread ? "conn" : tid)));
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
fputs("\n", stderr);
pthread_mutex_unlock(&trace_print_mutex);
}
// **** signalling between threads
void please_reopen_tcp()
// called by tcp_write_record, tcp_read_record on failure
{
if (trace & TRACE_SIGNALS)
tprintln("! please_reopen_tcp");
// signal the connection thread - don't care if nobody's waiting (already working on it, probably)
if (pthread_mutex_lock(&tcp_reconnect_mutex) < 0)
perror("pthread_mutex_lock(reconnect)");
if (pthread_cond_signal(&tcp_reconnect) < 0) {
perror("please_reopen_tcp\n");
exit(1);
}
if (pthread_mutex_unlock(&tcp_reconnect_mutex) < 0)
perror("pthread_mutex_unlock(reconnect)");
}
void wait_for_reconnect_signal()
// called by tcp_connector, tcp_listener
{
/* wait for someone to ask for reconnection, typically after an error in read/write */
if (trace & TRACE_SIGNALS)
tprintln("! wait_for_reconnect_signal...");
pthread_mutex_lock(&tcp_reconnect_mutex);
if (pthread_cond_wait(&tcp_reconnect, &tcp_reconnect_mutex) < 0) {
perror("wait_for_reconnect_signal");
exit(1);
}
if (pthread_mutex_unlock(&tcp_reconnect_mutex) < 0) {
perror("wait_for_reconnect_signal(unlock)");
exit(1);
}
if (trace & TRACE_SIGNALS)
tprintln("! wait_for_reconnect_signal done");
}
void inform_tcp_is_open()
{
if (trace & TRACE_SIGNALS)
tprintln("! inform_tcp_is_open");
if (pthread_mutex_lock(&tcp_is_open_mutex) < 0)
perror("pthread_mutex_lock(tcp_is_open)");
/* wake everyone waiting on this */
if (pthread_cond_broadcast(&tcp_is_open) < 0) {
perror("inform_tcp_is_open");
exit(1);
}
if (pthread_mutex_unlock(&tcp_is_open_mutex) < 0)
perror("pthread_mutex_unlock(tcp_is_open)");
}
void wait_for_tcp_open()
{
/* wait for tcp_is_open, and get a mutex lock */
if (trace & TRACE_SIGNALS)
tprintln("! wait_for_tcp_open...");
pthread_mutex_lock(&tcp_is_open_mutex);
if (trace & TRACE_SIGNALS)
tprintln("! wait_for_tcp_open got lock, waiting...");
if (pthread_cond_wait(&tcp_is_open, &tcp_is_open_mutex) < 0) {
perror("wait_for_tcp_open(wait)");
exit(1);
}
if (trace & TRACE_SIGNALS)
tprintln("! wait_for_tcp_open got signal");
if (pthread_mutex_unlock(&tcp_is_open_mutex) < 0) {
perror("wait_for_tcp_open(unlock)");
exit(1);
}
if (trace & TRACE_SIGNALS)
tprintln("! wait_for_tcp_open done");
}
// **** write a record length (two bytes MSB first) and that many bytes
#if UTTUN_TLS
int tcp_write_record(SSL *ssl, u_char *buf, int len)
#else
int tcp_write_record(int fd, u_char *buf, int len)
#endif
{
u_char reclen[2];
struct iovec iov[2];
int wrote;
if (len > 0xffff) {
fprintf(stderr,"tcp_write_record: too long: %#x > 0xffff\n", len);
exit(1);
}
if (len > UTTUN_TCP_MAXLEN) {
fprintf(stderr,"tcp_write_record: too long: %#x > %#x\n", len, UTTUN_TCP_MAXLEN);
exit(1);
}
#if UTTUN_TLS
u_char obuf[UTTUN_TCP_MAXLEN+2];
obuf[0] = (len >> 8) & 0xff;
obuf[1] = len & 0xff;
memcpy(obuf+2, buf, len);
if ((wrote = SSL_write(ssl, obuf, 2+len)) <= 0) {
int err = SSL_get_error(ssl, wrote);
// punt;
fprintf(stderr,"SSL_write error %d\n", err);
ERR_print_errors_fp(stderr);
#if 1 // debug
exit(1);
#else // probably what we want later
please_reopen_tcp();
#endif
}
#else
reclen[0] = (len >> 8) & 0xff;
reclen[1] = len & 0xff;
iov[0].iov_base = reclen;
iov[0].iov_len = 2;
iov[1].iov_base = buf;
iov[1].iov_len = len;
if ((wrote = writev(fd, iov, 2)) < 0) {
perror("writev");
/* close(fd); */
please_reopen_tcp();
}
#endif
else if (wrote != len+2)
fprintf(stderr,"tcp_write_record: wrote %d bytes != %d\n", wrote, len+2);
else if (trace & TRACE_TCP)
tprintln("> TCP sent %d bytes (reclen %d)", wrote, len);
return wrote;
}
// **** read a record length (two bytes MSB first) and that many bytes
#if UTTUN_TLS
int tcp_read_record(SSL *ssl, u_char *buf, int blen)
#else
int tcp_read_record(int fd, u_char *buf, int blen)
#endif
{
int cnt, rlen, actual;
u_char reclen[2];
#if UTTUN_TLS
cnt = SSL_read(ssl, reclen, 2);
#else
cnt = read(fd, reclen, 2);
#endif
if ((cnt) < 0) {
perror("read record length");
please_reopen_tcp();
return -1;
}
if (cnt == 0) {
// EOF
if (trace & TRACE_TCP)
tprintln("< 0 bytes read");
please_reopen_tcp();
return -1;
} else if (cnt != 2) {
if (trace & TRACE_TCP)
tprintln("< record len not 2: %d", cnt);
return 0;
}
rlen = reclen[0] << 8 | reclen[1]; //ntohs(reclen[0] << 8 || reclen[1]);
if (rlen == 0) {
if (trace & TRACE_TCP)
tprintln("< TCP MARK read");
return 0;
}
if (trace & TRACE_TCP)
tprintln("< TCP record len %d", rlen);
if (rlen > blen) {
tprintln("TCP record too long for buffer: %d > %d", rlen, blen);
please_reopen_tcp();
return -1;
}
#if UTTUN_TLS
actual = SSL_read(uttun_ssl, buf, rlen);
#else
actual = read(fd, buf, rlen);
#endif
if (actual < 0) {
perror("read record");
please_reopen_tcp();
return -1;
}
if (actual < rlen) {
if (trace & TRACE_TCP)
tprintln("< TCP read less than record: %d < %d", actual, rlen);
// read the remaining data
int p = actual;
while (rlen - p > 0) {
#if UTTUN_TLS
actual = SSL_read(uttun_ssl, &buf[p], rlen-p);
#else
actual = read(fd, &buf[p], rlen-p);
#endif
if (actual < 0) {
perror("re-read record");
please_reopen_tcp();
return -1;
}
if (trace & TRACE_TCP)
tprintln("< TCP read %d more bytes", actual);
if (actual == 0) {
please_reopen_tcp();
return -1;
}
p += actual;
}
actual = p;
}
if (trace & TRACE_TCP)
tprintln("< TCP read %d bytes total", actual);
if (trace & TRACE_PACKETS)
dumppkt(buf,actual);
return actual;
}
// **** low-level stuff
int tcp_server_accept(int sock)
{
struct sockaddr caddr;
int fd;
u_int clen = sizeof(struct sockaddr);
u_int *slen = &clen;
struct sockaddr *sa = &caddr;
if ((fd = accept(sock, (struct sockaddr *)sa, slen)) < 0) {
perror("accept");
fprintf(stderr,"errno = %d\n", errno);
// @@@@ better error handling, back off and try again? what could go wrong here?
exit(1);
}
// @@@@ log stuff about the connection
// @@@@ maybe validate/authenticate the other end
if (trace & TRACE_TCP) {
char ip6[INET6_ADDRSTRLEN];
if (inet_ntop(sa->sa_family,
(sa->sa_family == AF_INET ?
(void *)&((struct sockaddr_in *)sa)->sin_addr :
(void *)&((struct sockaddr_in6 *)sa)->sin6_addr),
ip6, sizeof(ip6)) == NULL)
strerror_r(errno,ip6,sizeof(ip6));
tprintln(" TCP accept connection from %s port %d", ip6, ntohs(((struct sockaddr_in *)sa)->sin_port));
}
return fd;
}
void tcp_server_listen(int sock)
{
if (listen(sock, UTTUN_BACKLOG) < 0) {
perror("listen");
exit(1);
}
}
int tcp_client_connect(struct sockaddr_in *sin)
{
int sock, unlucky = 1, foo;
while (unlucky) {
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket(tcp)");
exit(1);
}
if (connect(sock, (struct sockaddr *)sin, sizeof(struct sockaddr_in)) < 0) {
perror("connect");
// @@@@ better error handling, back off and try again - the other end might be down
if (trace & TRACE_TCP)
tprintln(" TCP trying again in %d s", unlucky);
if ((foo = sleep(unlucky)) != 0)
tprintln(" TCP sleep returned %d", foo);
unlucky++;
if (unlucky > 30) unlucky /= 3;
if (close(sock) < 0)
perror("close(tcp_client_connect)");
continue;
}
else
unlucky = 0;
}
// @@@@ log stuff about the connection
if (trace & TRACE_TCP)
tprintln(" TCP connected to %s port %d",
inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
return sock;
}
int bind_socket(int type, u_short port)
{
int sock;
struct sockaddr_in sin;
if ((sock = socket(AF_INET, type, 0)) < 0) {
perror("socket failed");
exit(1);
}
// @@@@ SO_REUSEADDR or SO_REUSEPORT
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0)
perror("setsockopt(SO_REUSEADDR)");
if (trace)
tprintln(" binding socket type %d (%s) to port %d",
type, (type == SOCK_DGRAM ? "dgram" : (type == SOCK_STREAM ? "stream" : "?")),
port);
memset(&sin,0,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = INADDR_ANY;
if (bind(sock,(struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("bind() failed");
exit(1);
}
return sock;
}
void
udp_send_pkt(int sock, struct sockaddr_in *sout, unsigned char *buf, int len)
{
ssize_t n;
if ((n = sendto(sock, buf, len, 0, (struct sockaddr *)sout, sizeof(struct sockaddr_in))) < 0) {
perror("sendto failed");
exit(1);
}
if (trace & TRACE_UDP)
tprintln("> UDP send %d bytes to %s port %d", n, inet_ntoa(sout->sin_addr), ntohs(sout->sin_port));
}
int
udp_receive(int sock, unsigned char *buf, int buflen)
{
struct sockaddr_in sin;
u_int sinlen;
int cnt;
memset(&sin,0,sizeof(sin));
sinlen = sizeof(sin);
cnt = recvfrom(sock, buf, buflen, 0, (struct sockaddr *)&sin, &sinlen);
if (cnt < 0) {
perror("recvfrom");
exit(1);
}
if (trace & TRACE_UDP)
tprintln("< UDP read %d bytes from %s port %d", cnt,
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
if (trace & TRACE_PACKETS)
dumppkt(buf,cnt);
return cnt;
}
#if UTTUN_TLS
void init_openssl()
{
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
}
void cleanup_openssl()
{
EVP_cleanup();
}
SSL_CTX *create_some_context(const SSL_METHOD *method)
{
SSL_CTX *ctx;
ctx = SSL_CTX_new(method);
if (!ctx) {
perror("Unable to create SSL context");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
return ctx;
}
SSL_CTX *create_client_context()
{
const SSL_METHOD *method;
method = SSLv23_client_method();
return create_some_context(method);
}
SSL_CTX *create_server_context()
{
const SSL_METHOD *method;
method = SSLv23_server_method();
return create_some_context(method);
}
void configure_context(SSL_CTX *ctx)
{
// Auto-select elliptic curve
SSL_CTX_set_ecdh_auto(ctx, 1);
/* Set the key and cert */
if (SSL_CTX_use_certificate_file(ctx, tls_cert_file, SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
if (SSL_CTX_use_PrivateKey_file(ctx, tls_key_file, SSL_FILETYPE_PEM) <= 0 ) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
// Check key and cert
if (SSL_CTX_check_private_key(ctx) != 1) {
fprintf(stderr,"Private key and certificate do not match\n");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
#if UTTUN_TLS_VERIFY
// Load CA cert chain
if (!SSL_CTX_load_verify_locations(ctx, tls_ca_file, NULL)) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
// Make sure to verify the peer (both server and client)
// Consider adding a callback to validate CN/subjectAltName?
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
// go up to "higher level CA"
SSL_CTX_set_verify_depth(ctx, 2);
#endif
}
void
describe_tls_cert(X509 *cert)
{
X509_NAME *subj, *issuer;
ASN1_TIME *notafter;
subj = X509_get_subject_name(cert);
issuer = X509_get_issuer_name(cert);
notafter = X509_get_notAfter(cert);
tprintln("TLS cert Subject:");
X509_NAME_print_ex_fp(stderr, subj, 2, XN_FLAG_ONELINE);
fprintf(stderr,"\n");
tprintln("TLS cert Issuer:");
X509_NAME_print_ex_fp(stderr, issuer, 2, XN_FLAG_ONELINE);
fprintf(stderr,"\n");
tprintln("TLS cert expires:");
// What an additional complexity
BIO *b = BIO_new_fp(stderr, BIO_NOCLOSE);
BIO_puts(b, " ");
ASN1_TIME_print(b, notafter);
BIO_puts(b, "\n");
BIO_free(b);
}
// this only checks there is a CN which is not totally bogus.
// Maybe let it check the CN/subjectAltName against a configured/expected such.
int
validate_cn(X509 *cert)
{
// see https://github.com/iSECPartners/ssl-conservatory/blob/master/openssl/openssl_hostname_validation.c
int common_name_loc = -1, subj_alt_name_loc = -1;
X509_NAME_ENTRY *common_name_entry = NULL, *subj_alt_name_entry = NULL;
ASN1_STRING *common_name_asn1 = NULL, *subj_alt_name_asn1 = NULL;
char *common_name_str = NULL, *subj_alt_name_str = NULL;
// Find the position of the CN field in the Subject field of the certificate
common_name_loc = X509_NAME_get_index_by_NID(X509_get_subject_name((X509 *) cert), NID_commonName, -1);
if (common_name_loc < 0) {
if (trace & TRACE_TLS)
tprintln("TLS validate_cn: can't find CN");
return 0;
}
// Extract the CN field
common_name_entry = X509_NAME_get_entry(X509_get_subject_name((X509 *) cert), common_name_loc);
if (common_name_entry == NULL) {
if (trace & TRACE_TLS)
tprintln("TLS validate_cn: can't extract CN");
return 0;
}
// Convert the CN field to a C string
common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);
if (common_name_asn1 == NULL) {
if (trace & TRACE_TLS)
tprintln("TLS validate_cn: can't convert CN to C string");
return 0;
}
#if OPENSSL_VERSION_NUMBER < 0x10100000L
common_name_str = (char *) ASN1_STRING_data(common_name_asn1);
#else
common_name_str = (char *) ASN1_STRING_get0_data(common_name_asn1);
#endif
// Make sure there isn't an embedded NUL character in the CN
if (ASN1_STRING_length(common_name_asn1) != strlen(common_name_str)) {
if (trace & TRACE_TLS)
tprintln("TLS validate_cn: malformed CN (NUL in CN)");
return 0; // MalformedCertificate;
}
// same for NID_subject_alt_name? But too hairy to include in cert?
// maybe not with openssl 1.1.1, see https://security.stackexchange.com/a/183973
if (trace & TRACE_TLS)
tprintln("TLS: found CN %s", common_name_str);
// Find Chaosnet addr of CN through DNS:
// this is just too much work with a poorly documented library (libresolv)
// which also seems to think it needs to connect over chaosnet to find chaosnet data.
// Instead, can we find the subjectAltName and let that be just an int 0x701 etc?
// Seems too hairy (yet) to include it in cert.
// Just trust it, but maybe log CN anyway?
// Stuff it in link datastructure, or compare to a configured token there?
return 1; // success
}
#endif // UTTUN_TLS
// **** read from UDP, send on TCP
void *udp_listener(void *arg)
{
int rlen, wlen;
u_char ubuf[UTTUN_UDP_MAXLEN];
// wait for initial open
wait_for_tcp_open();
while (1) {
rlen = udp_receive(usock, (u_char *)&ubuf, sizeof(ubuf));
// @@@@ make sure it's from the right source? here or in udp_receive
if (rlen > 0) {
#if UTTUN_TLS
wlen = tcp_write_record(uttun_ssl, (u_char *)&ubuf, rlen);
#else
wlen = tcp_write_record(tsock, (u_char *)&ubuf, rlen);
#endif
if (wlen < 0) {
// error. tcp_write_record already closed and asked for reopen.
// wait for it to happen.
wait_for_tcp_open();
}
}
}
}
// **** read from TCP, send on UDP
void *tcp_reader(void *arg)
{
int rlen;
u_char tbuf[UTTUN_TCP_MAXLEN];
// wait for initial open
wait_for_tcp_open();
while (1) {
#if UTTUN_TLS
rlen = tcp_read_record(uttun_ssl, (u_char *)&tbuf, sizeof(tbuf));
#else
rlen = tcp_read_record(tsock, (u_char *)&tbuf, sizeof(tbuf));
#endif
if (rlen > 0) {
udp_send_pkt(usock, &udpdest, (u_char *)&tbuf, rlen);
} else if (rlen < 0) {
// error. tcp_read_record already closed and asked for reopen.
// wait for it to happen.
wait_for_tcp_open();
}
}
}
// **** keep connected
void *tcp_listener(void *arg)
{
tcp_server_listen(tursock);
sleep(1); // make sure the other threads are waiting
#if UTTUN_TLS
SSL *ssl;
SSL_CTX *ctx = create_server_context();
configure_context(ctx);
#endif
while (1) {
tsock = tcp_server_accept(tursock);
#if UTTUN_TLS
if ((ssl = SSL_new(ctx)) == NULL) {
fprintf(stderr,"SSL_new failed\n");
ERR_print_errors_fp(stderr);
close(tsock);
continue;
}
SSL_set_fd(ssl, tsock);
int v = 0;
if ((v = SSL_accept(ssl)) <= 0) {
// this already catches verification - client end gets "SSL alert number 48"?
int err = SSL_get_error(ssl, v);
tprintln("SSL_accept failed: %d", err);
if ((err != SSL_ERROR_SSL) && (trace & TRACE_TLS)) {
ERR_print_errors_fp(stderr);
//1975129200:error:1417C086:SSL routines:tls_process_client_certificate:certificate verify failed:../ssl/statem/statem_srvr.c:2893:
}
X509 *ssl_client_cert = SSL_get_peer_certificate(ssl);
if (ssl_client_cert) {
long verifyresult = SSL_get_verify_result(ssl);
if (trace & TRACE_TLS) {
if(verifyresult != X509_V_OK) {
tprintln("TLS client certificate verification failed");
} else {
tprintln("TLS client verification succeeded when SSL_accept failed???");
}
// describe cert
describe_tls_cert(ssl_client_cert);
}
} else {
// this seems to be the case (for self-signed cert)
if (trace & TRACE_TLS)
tprintln("TLS: There is no client certificate - closing connection");
}
close(tsock);
SSL_free(ssl);
// ideally, back off?
continue;
}
#if UTTUN_TLS_VERIFY
X509 *ssl_client_cert = NULL;
ssl_client_cert = SSL_get_peer_certificate(ssl);
if(ssl_client_cert) {
long verifyresult;
verifyresult = SSL_get_verify_result(ssl);
if(verifyresult != X509_V_OK) {
if (trace & TRACE_TLS) {
tprintln("TLS client certificate verification failed - closing connection");
// describe cert
describe_tls_cert(ssl_client_cert);
}
X509_free(ssl_client_cert);
SSL_free(ssl);
uttun_ssl = NULL;
close(tsock);
continue;
} else if (trace & TRACE_TLS) {
tprintln("TLS client verification succeeded");
// describe cert
describe_tls_cert(ssl_client_cert);
// @@@@ find CN and verify it
}
int valid = validate_cn(ssl_client_cert);
X509_free(ssl_client_cert);
if (!valid) {
if (trace & TRACE_TLS)
tprintln("TLS client CN invalid");
continue;
}
}
else {
if (trace & TRACE_TLS)
tprintln("TLS: There is no client certificate - closing connection\n");
SSL_free(ssl);
uttun_ssl = NULL;
close(tsock);
continue;
}
#endif
uttun_ssl = ssl;
#endif
inform_tcp_is_open();
wait_for_reconnect_signal();
#if UTTUN_TLS
SSL_free(ssl);
uttun_ssl = NULL;
#endif
#if 1
// should already be closed, ignore return
close(tsock);
#else
if (close(tsock) < 0)
perror("close(tcp)");
#endif
}
}
void *tcp_connector(void *arg)
{
#if UTTUN_TLS
SSL *ssl;
SSL_CTX *ctx = create_client_context();
configure_context(ctx);
#endif
while (1) {
// @@@@ re-parse tcpdest?
// connect to server
tsock = tcp_client_connect(&tcpdest);
#if UTTUN_TLS
if ((ssl = SSL_new(ctx)) == NULL) {
tprintln("SSL_new failed");
ERR_print_errors_fp(stderr);
close(tsock);
continue;
}
SSL_set_fd(ssl, tsock);
if (SSL_connect(ssl) <= 0) {
ERR_print_errors_fp(stderr);
SSL_free(ssl);
close(tsock);
continue;
}
#if UTTUN_TLS_VERIFY
X509 *ssl_server_cert = NULL;
ssl_server_cert = SSL_get_peer_certificate(ssl);
if(ssl_server_cert) {
long verifyresult;
verifyresult = SSL_get_verify_result(ssl);
if(verifyresult != X509_V_OK) {
if (trace & TRACE_TLS) {
tprintln("TLS server certificate verification failed - closing connection");
// describe cert
describe_tls_cert(ssl_server_cert);
}
X509_free(ssl_server_cert);
SSL_free(ssl);
uttun_ssl = NULL;
close(tsock);
continue;
} else if (trace & TRACE_TLS) {
tprintln("TLS server verification succeeded");
// describe cert
describe_tls_cert(ssl_server_cert);
}
int valid = validate_cn(ssl_server_cert);
X509_free(ssl_server_cert);
if (!valid) {
if (trace & TRACE_TLS)
tprintln("TLS client CN invalid");
continue;
}
}
else {
if (trace & TRACE_TLS)
tprintln("There is no server certificate - closing connection");
SSL_free(ssl);
uttun_ssl = NULL;
close(tsock);
continue;
}
#endif
uttun_ssl = ssl;
#endif
// tell others about it
inform_tcp_is_open();
// wait for someone to ask us to reconnect
wait_for_reconnect_signal();
// make sure it's closed, try to tell the other end
if (close(tsock) < 0)
; // should already be closed, ignore.
// perror("close(tcp)");
#if UTTUN_TLS
uttun_ssl = NULL;
SSL_free(ssl);
#endif
}
}
// **** main stuff
int parse_dest(char *token, u_short *port, u_short *myport, struct sockaddr_in *sin)
{
// @@@@ rewrite using gettaddrinfo
struct hostent *he;
struct in_addr ip;
char s[256];
int val, a, b;
if (trace > 0)
tprintln("parsing host spec %s", token);
if (sscanf(token, "%d:%[^:]:%d", &a, (char *)&s, &b) == 3) {
if ((he = gethostbyname(s)) == NULL) {
fprintf(stderr,"bad host '%s'\n", s);
return -1;
}
tprintln("parsed myport:host:port %d:%s:%d", a, s, b);
*myport = a;
*port = b;
} else if (sscanf(token, "%d:%s", &a, (char *)&s) == 2) {
if ((he = gethostbyname(s)) == NULL) {
fprintf(stderr,"bad host '%s' (d:s)\n", s);
return -1;
}
tprintln("parsed myport:host %d:%s", a, s);
*myport = a;
} else if (sscanf(token,"%[^:]:%d", (char *)&s, &b) == 2) {
if ((he = gethostbyname(s)) == NULL) {
fprintf(stderr,"bad host '%s' (s:d)\n", s);
return -1;
}
tprintln("parsed host:port %s:%d", s, b);
*port = b;
} else if ((inet_aton(token, &ip) == 1) &&
(inet_netof(ip) != 0) &&
(inet_lnaof(ip) != 0)) {
if ((he = gethostbyname(token)) == NULL) {
fprintf(stderr,"bad host '%s' (ip)\n", token);
return -1;
}
tprintln("parsed host %s", token);