forked from RandyGaul/cute_headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cute_tls.h
1303 lines (1119 loc) · 43.8 KB
/
cute_tls.h
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
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
cute_tls.h - v1.01
To create implementation (the function definitions)
#define CUTE_TLS_IMPLEMENTATION
in *one* C/CPP file (translation unit) that includes this file
SUMMARY
cute_tls.h is a single-file header that implements some functions to
make a connection over TLS 1.2 and send some data back and forth over a
TCP socket. It's meant mainly for making some simple HTTPS requsts to a web
server, but nothing heavy-duty requiring extreme performance. It uses native
APIs on Windows and Apple machines to get access to highly robust TLS
implementations for security. This header merely facilitates these native APIs
through a TCP socket.
On Windows Secure Channel is used. On Apple machines the Network.framework is used.
For *Nix s2n can be used a third-party solution, since no good OS-level TLS
handshake is currently available on Linux.
GENERAL INFORMATION ABOUT HTTPS
This header is great for providing a TLS tunnel to hook up to your favorite
https writer. Simply pipe the finalized HTTP buffer through a TLS connection
created by this header, and boom -- you have HTTPS.
The general theory behind how it works: websites out in the world ask a special
entity called a Certificate Authority (CA) to recognize them as a valid website.
The CA gives them a certificate (usually an X.509 cert). It usually lasts for some
number of years, and is used to validate that a message came from the website you
think it did. This prevents anyone from pretending to who they are not, and also
prevents anyone from reading or tampering with packets between yourself and the website.
This works by requesting the website provide you with their certificate. Everyone's
machines have built-in mechanisms to ask a trusted CA if the website's certificate
is valid. These are usually installed along with the operating system or browser.
HTTPS is HTTP over the TLS protocol, using high-powered cryptography via certificates.
By opening a connection with cute_tls.h we immediately have access to making HTTPS
requests!
LIMITATIONS
Emscripten/Android platforms are not currently supported.
Client credentials are not supported.
IPv6 is not supported, though this is totally possible, just not initially in v1.00.
The server side of the connection is *not* supported. This is a client-only implementation.
BUILDING ON APPLE DEVICES
To build on Apple devices you must compile this file as an Objective-C file (.m). This is
kind of icky and super lame, but blame Apple for continually deprecating their C APIs and
releasing newer Obj-C replacements.
Link against Network.framework, and Security.framework. Make sure automatic reference
counting (ARC) is off if you're using XCode.
Example command line:
clang -framework Network main.cpp -o my_executable
BUILDING ON *NIX (INCLUDING APPLE DEVICES)
A wrapping implementation of s2n is implemented, see: https://github.com/aws/s2n-tls
s2n is a pretty good library for implementing TLS servers, but can also be used for
clients. It's quite a good library for Apple/Linux, but has no Windows support. Since
Linux has no out-of-the-box TLS handshake implementation at the OS-level, unlike Apple/Windows,
we have to choose some third-party tool to get things done. Another decent choice could
have been mbedtls, but s2n is implemented with much less code, and was thus chosen instead.
If on MacOS for development make sure to install s2n on your machine. I suggest using brew.
brew install s2n
Regardless of how you install s2n make sure your compiler can find the static library and
headers in order to properly link against s2n. Here's an example implementation for devloping
on MacOS: https://github.com/RandyGaul/cute_headers/tree/master/examples_cute_tls/macos/s2n
For Linux it's basically he same story. If you're using Linux I assume you know what you're
doing, so follow along on the s2n docs as you see fit: https://github.com/aws/s2n-tls
OTHER OPERATING SYSTEMS (e.g. ANDROID/EMSCRIPTEN)
Android is a bit of another story. I don't have too much Android dev experience, but the best
option may be to call from C into Java and use Android's on SSL socket, or their HTTPS APi
directly from Java: https://developer.android.com/training/articles/security-ssl
For Emscripten it looks like they have a C++ websocket wrapper that might work, though I
have yet to try it: https://emscripten.org/docs/porting/networking.html#emscripten-websockets-api
CUSTOMIZATION
A variety of macros can be overriden by merely defining them before including the implementation
section of this file. Define any one of them to use your own custom functions.
TLS_MALLOC
TLS_MEMCPY
TLS_MEMSET
TLS_MEMMOVE
TLS_ASSERT
TLS_STRCMP
TLS_PACKET_QUEUE_MAX_ENTRIES (default at 64)
SPECIAL THANKS
A special thanks goes to Martins Mozeiko for their sample code on setting up a TLS
connection via SChannel: https://gist.github.com/mmozeiko/c0dfcc8fec527a90a02145d2cc0bfb6d
A special thanks goes to Mattias Gustavsson for their API design in https.h:
https://github.com/mattiasgustavsson/libs/blob/main/http.h
Revision history:
1.00 (06/17/2023) initial release
1.01 (06/19/2023) s2n implementation for apple/linux
*/
/*
DOCUMENTATION
1. Call tls_connect
TLS_Connection connection = tls_connect(hostname, 443);
2. Call tls_process
while (1) {
TLS_State state = tls_process(connection);
...
}
3. Call tls_read or tls_send
tls_send(connection, buffer, size);
or
tls_read(connection, buffer, size);
4. Call tls_disconnect
5. For errors call tls_state_string on the return value of tls_process.
FULL DEMO PROGRAM
Here's a full program that connects to a given website and sends an HTTP GET request.
The entire HTTP response is saved to a file called "response.txt". You may also try using
badssl.com to try out a variety of failure cases (the commented strings at the top of the
demo function).
#define CUTE_TLS_IMPLEMENTATION
#include "cute_tls.h"
int main()
{
const char* hostname = "www.google.com";
//const char* hostname = "badssl.com";
//const char* hostname = "expired.badssl.com";
//const char* hostname = "wrong.host.badssl.com";
//const char* hostname = "self-signed.badssl.com";
//const char* hostname = "untrusted-root.badssl.com";
TLS_Connection connection = tls_connect(hostname, 443);
while (1) {
TLS_State state = tls_process(connection);
if (state == TLS_STATE_CONNECTED) {
break;
} else if (state < 0) {
printf("Error connecting to to %s with code %s.\n", hostname, tls_state_string(state));
return -1;
}
}
printf("Connected!\n");
// Send GET request.
char req[1024];
int len = sprintf(req, "GET / HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", hostname);
if (tls_send(connection, req, len) < 0) {
tls_disconnect(connection);
printf("Failed to send data.\n");
return -1;
}
// Write the full HTTP response to file.
FILE* fp = fopen("response.txt", "wb");
int received = 0;
char buf[TLS_MAX_PACKET_SIZE];
while (1) {
TLS_State state = tls_process(connection);
if (state == TLS_STATE_DISCONNECTED) {
break;
}
int bytes = tls_read(connection, buf, sizeof(buf));
if (bytes < 0) {
tls_disconnect(connection);
printf("Failed reading bytes.\n");
return -1;
}
if (bytes) {
fwrite(buf, 1, bytes, fp);
fflush(fp);
received += bytes;
}
}
fclose(fp);
printf("Received %d bytes\n", received);
tls_disconnect(connection);
}
*/
#ifndef CUTE_TLS_H
#define CUTE_TLS_H
typedef struct TLS_Connection { unsigned long long id; } TLS_Connection;
// Initiates a new TLS 1.2 connection.
TLS_Connection tls_connect(const char* hostname, int port);
// Frees up all resources associated with the connection.
void tls_disconnect(TLS_Connection connection);
typedef enum TLS_State
{
TLS_STATE_BAD_CERTIFICATE = -8, // Bad or unsupported cert format.
TLS_STATE_SERVER_ASKED_FOR_CLIENT_CERTS = -7, // Not supported.
TLS_STATE_CERTIFICATE_EXPIRED = -6,
TLS_STATE_BAD_HOSTNAME = -5,
TLS_STATE_CANNOT_VERIFY_CA_CHAIN = -4,
TLS_STATE_NO_MATCHING_ENCRYPTION_ALGORITHMS = -3,
TLS_STATE_INVALID_SOCKET = -2,
TLS_STATE_UNKNOWN_ERROR = -1,
TLS_STATE_DISCONNECTED = 0,
TLS_STATE_DISCONNECTED_BUT_PACKETS_STILL_REMAIN = 1, // The TCP socket closed, but you should keep calling `tls_read`.
TLS_STATE_PENDING = 2, // Handshake in progress.
TLS_STATE_CONNECTED = 3,
TLS_STATE_PACKET_QUEUE_FILLED = 4, // Not calling `tls_read` enough. Did you forget to call this in a loop after `tls_process`?
} TLS_State;
// Call this in a loop to update the connection.
// This will perform the initial connect sequence, and also fetch data off the wire once connected.
TLS_State tls_process(TLS_Connection connection);
// Returns `TLS_State` as a string.
const char* tls_state_string(TLS_State state);
// Returns number of bytes read on success, -1 on failure.
int tls_read(TLS_Connection connection, void* data, int size);
// Reads up to size bytes, returns amount of bytes received on success (<= size).
// Returns 0 on disconnect or -1 on error.
int tls_send(TLS_Connection connection, const void* data, int size);
#define TLS_1_KB 1024
#define TLS_MAX_RECORD_SIZE (16 * TLS_1_KB) // TLS defines records to be up to 16kb.
#define TLS_MAX_PACKET_SIZE (TLS_MAX_RECORD_SIZE + TLS_1_KB) // Some extra rooms for records split over two packets.
#endif // CUTE_TLS_H
#ifdef CUTE_TLS_IMPLEMENTATION
#ifndef CUTE_TLS_IMPLEMENTATION_ONCE
#define CUTE_TLS_IMPLEMENTATION_ONCE
#ifndef _CRT_SECURE_NO_WARNINGS
# define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_NONSTDC_NO_DEPRECATE
# define _CRT_NONSTDC_NO_DEPRECATE
#endif
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
# define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
#ifdef CUTE_TLS_S2N
# define TLS_S2N
#endif
#ifndef TLS_S2N
# ifdef _WIN32
# define TLS_WINDOWS
# elif defined(__APPLE__)
# define TLS_APPLE
# elif defined(__linux__) || defined(__unix__) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__)
# define TLS_S2N
# else
# error Platform not yet supported.
# endif
#endif
#ifdef TLS_WINDOWS
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <winsock2.h>
# include <ws2tcpip.h>
# include <windows.h>
# ifndef SECURITY_WIN32
# define SECURITY_WIN32
# endif
# include <security.h>
# include <schannel.h>
# include <shlwapi.h>
# include <assert.h>
# include <stdio.h>
# pragma comment (lib, "ws2_32.lib")
# pragma comment (lib, "secur32.lib")
# pragma comment (lib, "shlwapi.lib")
#elif defined(TLS_APPLE)
# include <Network/Network.h>
# include <pthread.h>
#elif defined(TLS_S2N)
# include <assert.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <sys/socket.h>
# include <fcntl.h>
# include <unistd.h>
# include <netdb.h>
# include <errno.h>
# include <s2n.h>
#else
# error No supported backend implementation found.
#endif
#define TLS_MIN(x, y) ((x) < (y) ? (x) : (y))
#define TLS_ARRAYSIZE(A) (sizeof(A) / sizeof(*A))
#ifndef TLS_MALLOC
# define TLS_MALLOC malloc
# define TLS_FREE free
#endif
#ifndef TLS_MEMCPY
# define TLS_MEMCPY memcpy
#endif
#ifndef TLS_MEMSET
# define TLS_MEMSET memset
#endif
#ifndef TLS_MEMMOVE
# define TLS_MEMMOVE memmove
#endif
#ifndef TLS_ASSERT
# define TLS_ASSERT assert
#endif
#ifndef TLS_STRCMP
# define TLS_STRCMP strcmp
#endif
#ifndef TLS_STRNCMP
# define TLS_STRNCMP strncmp
#endif
#ifndef TLS_PACKET_QUEUE_MAX_ENTRIES
# define TLS_PACKET_QUEUE_MAX_ENTRIES (64)
#endif
typedef struct TLS_PacketQueue
{
#ifdef TLS_APPLE
pthread_mutex_t lock;
#endif
int count;
int index0;
int index1;
int sizes[TLS_PACKET_QUEUE_MAX_ENTRIES];
void* packets[TLS_PACKET_QUEUE_MAX_ENTRIES];
} TLS_PacketQueue;
static void tls_packet_queue_push(TLS_PacketQueue* q, void* packet, int size)
{
#ifdef TLS_APPLE
pthread_mutex_lock(&q->lock);
#endif
if (q->count < TLS_PACKET_QUEUE_MAX_ENTRIES) {
q->count++;
q->sizes[q->index1] = size;
q->packets[q->index1] = packet;
q->index1 = (q->index1 + 1) % TLS_PACKET_QUEUE_MAX_ENTRIES;
}
#ifdef TLS_APPLE
pthread_mutex_unlock(&q->lock);
#endif
}
void tls_packet_queue_pop(TLS_PacketQueue* q, void** packet, int* size)
{
#ifdef TLS_APPLE
pthread_mutex_lock(&q->lock);
#endif
if (q->count > 0) {
q->count--;
*size = q->sizes[q->index0];
*packet = q->packets[q->index0];
q->index0 = (q->index0 + 1) % TLS_PACKET_QUEUE_MAX_ENTRIES;
}
#ifdef TLS_APPLE
pthread_mutex_unlock(&q->lock);
#endif
}
typedef struct TLS_Context
{
TLS_PacketQueue q; // For receiving packets.
TLS_State state; // Current state of the connection. Negative values are errors.
const char* hostname; // Website or address to connect to.
void* packet;
int packet_size;
#ifdef TLS_WINDOWS
SOCKET sock;
CredHandle handle;
CtxtHandle context;
SecPkgContext_StreamSizes sizes;
bool tcp_connect_pending;
bool first_call;
int received; // Byte count in incoming buffer (ciphertext).
int used; // Byte count used from incoming buffer to decrypt current packet.
int available; // Byte count available for decrypted bytes.
char* decrypted; // Points to incoming buffer where data is decrypted in-place.
char incoming[TLS_MAX_PACKET_SIZE];
#elif defined(TLS_S2N)
int sock;
struct s2n_connection* connection;
bool tcp_connect_pending;
int received;
char incoming[TLS_MAX_PACKET_SIZE];
#elif defined(TLS_APPLE)
dispatch_queue_t dispatch;
nw_connection_t connection;
#endif
} TLS_Context;
#ifdef TLS_S2N
int tls_s2n_init = 0;
#endif
// Called in a poll-style manner on Windows.
// For Apple we call this once on init to setup a tail-end recursive callback loop.
static void tls_recv(TLS_Context* ctx)
{
#ifdef TLS_WINDOWS
fd_set sockets_to_check;
FD_ZERO(&sockets_to_check);
FD_SET(ctx->sock, &sockets_to_check);
struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 0;
while (select((int)(ctx->sock + 1), &sockets_to_check, NULL, NULL, &timeout) == 1) {
int r = recv(ctx->sock, ctx->incoming + ctx->received, sizeof(ctx->incoming) - ctx->received, 0);
if (r == 0) {
// Server disconnected the socket.
ctx->state = TLS_STATE_DISCONNECTED;
break;
} else if (r == SOCKET_ERROR) {
// Socket related error.
ctx->state = TLS_STATE_INVALID_SOCKET;
break;
} else {
ctx->received += r;
}
}
#endif // TLS_WINDOWS
#ifdef TLS_APPLE
// Queue up an asynchronous receive block loop.
nw_connection_receive(ctx->connection, 1, TLS_MAX_PACKET_SIZE, ^(dispatch_data_t content, nw_content_context_t context, bool is_complete, nw_error_t receive_error) {
if (content != NULL) {
// What a horrid API design... So over-engineered to simply memcpy a buffer.
int size = (int)dispatch_data_get_size(content);
void* packet = TLS_MALLOC(size);
dispatch_data_apply(content, ^bool(dispatch_data_t content, size_t offset, const void* buffer, size_t size) {
TLS_MEMCPY((char*)packet + offset, buffer, size);
return true;
});
tls_packet_queue_push(&ctx->q, packet, size);
}
if (is_complete && !receive_error) {
ctx->state = TLS_STATE_DISCONNECTED_BUT_PACKETS_STILL_REMAIN;
} else if (receive_error) {
ctx->state = TLS_STATE_UNKNOWN_ERROR;
} else {
// Queue up another call to this function to receive the next packet.
tls_recv(ctx);
}
});
#endif // TLS_APPLE
#ifdef TLS_S2N
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
int bytes_read = 0;
while (bytes_read < sizeof(ctx->incoming)) {
int r = s2n_recv(ctx->connection, ctx->incoming + bytes_read, sizeof(ctx->incoming) - bytes_read, &blocked);
s2n_error_type etype = (s2n_error_type)s2n_error_get_type(s2n_errno);
if (r == 0) {
break;
} else if (r > 0) {
bytes_read += r;
} else if (etype == S2N_ERR_T_CLOSED) {
ctx->state = TLS_STATE_DISCONNECTED;
break;
} else if (etype == S2N_ERR_T_IO) {
ctx->state = TLS_STATE_INVALID_SOCKET;
break;
} else if (etype != S2N_ERR_T_BLOCKED) {
ctx->state = TLS_STATE_UNKNOWN_ERROR;
break;
}
}
ctx->received = bytes_read;
#endif // TLS_S2N
}
TLS_Connection tls_connect(const char* hostname, int port)
{
TLS_Connection result = { 0 };
TLS_Context* ctx = (TLS_Context*)TLS_MALLOC(sizeof(TLS_Context));
TLS_MEMSET(ctx, 0, sizeof(*ctx));
ctx->hostname = hostname;
char sport[64];
snprintf(sport, sizeof(sport), "%u", port);
#if defined(TLS_WINDOWS) || defined(TLS_S2N)
ctx->tcp_connect_pending = 1;
#ifdef TLS_WINDOWS
ctx->first_call = 1;
// Initialize winsock.
WSADATA wsadata;
if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0) {
TLS_FREE(ctx);
return result;
}
#elif defined(TLS_S2N)
if (!tls_s2n_init) {
tls_s2n_init = 1;
s2n_init();
}
#endif
#endif // defined(TLS_WINDOWS) || defined(TLS_S2N)
#if defined(TLS_WINDOWS) || defined(TLS_S2N)
// Perform DNS lookup.
struct addrinfo hints;
TLS_MEMSET(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
struct addrinfo* addri = NULL;
if (getaddrinfo(hostname, sport, &hints, &addri) != 0) {
freeaddrinfo(addri);
TLS_FREE(ctx);
return result;
}
// Create a TCP IPv4 socket.
ctx->sock = socket(AF_INET, SOCK_STREAM, 0);
if (ctx->sock == -1) {
freeaddrinfo(addri);
TLS_FREE(ctx);
return result;
}
// Set non-blocking IO.
{
#ifdef TLS_WINDOWS
DWORD non_blocking = 1;
int res = ioctlsocket(ctx->sock, FIONBIO, &non_blocking);
#else
int flags = fcntl(ctx->sock, F_GETFL, 0);
int res = fcntl(ctx->sock, F_SETFL, flags | O_NONBLOCK);
#endif
if (res != 0) {
#ifdef TLS_WINDOWS
closesocket(ctx->sock);
#else
close(ctx->sock);
#endif
freeaddrinfo(addri);
TLS_FREE(ctx);
return result;
}
}
// Startup the TCP connection.
if (connect(ctx->sock, addri->ai_addr, (int)addri->ai_addrlen) == -1) {
#ifdef TLS_WINDOWS
int error = WSAGetLastError();
if (error != WSAEWOULDBLOCK && error != WSAEINPROGRESS) {
freeaddrinfo(addri);
closesocket(ctx->sock);
TLS_FREE(ctx);
return result;
}
#else
if (errno != EWOULDBLOCK && errno != EINPROGRESS && errno != EAGAIN) {
freeaddrinfo(addri);
close(ctx->sock);
TLS_FREE(ctx);
return result;
}
#endif
} else {
freeaddrinfo(addri);
addri = NULL;
}
ctx->state = TLS_STATE_PENDING;
#ifdef TLS_WINDOWS
// Initialize a credentials handle for Secure Channel.
// This is needed for InitializeSecurityContextA.
{
SCHANNEL_CRED cred = { 0 };
cred.dwVersion = SCHANNEL_CRED_VERSION;
cred.dwFlags = SCH_USE_STRONG_CRYPTO // Disable deprecated or otherwise weak algorithms (on as default).
| SCH_CRED_AUTO_CRED_VALIDATION // Automatically validate server cert (on as default), as opposed to manual verify.
| SCH_CRED_NO_DEFAULT_CREDS; // Client certs are not supported.
cred.grbitEnabledProtocols = SP_PROT_TLS1_2; // Specifically pick only TLS 1.2.
if (AcquireCredentialsHandleA(NULL, (char*)UNISP_NAME_A, SECPKG_CRED_OUTBOUND, NULL, &cred, NULL, NULL, &ctx->handle, NULL) != SEC_E_OK)
{
closesocket(ctx->sock);
TLS_FREE(ctx);
return result;
}
}
#elif defined(TLS_S2N)
// Create connection and set our socket onto it. s2n wraps our socket -- actually
// a pretty cool API design, which actually simplifies send/recv in our implementation.
struct s2n_connection* connection = s2n_connection_new(S2N_CLIENT);
if (!connection) {
close(ctx->sock);
TLS_FREE(ctx);
return result;
}
if (s2n_connection_set_fd(connection, ctx->sock) < 0) {
close(ctx->sock);
TLS_FREE(ctx);
return result;
}
ctx->connection = connection;
// Disable client certs (not supported).
s2n_connection_set_client_auth_type(connection, S2N_CERT_AUTH_NONE);
// Make sure the cert hostname matches our expectation. If this isn't set here
// the connection will fail to validate. We could also use `s2n_connection_set_verify_host_callback`
// but this makes use of a built-in default callback (as per s2n docs). This was *not* at all
// clear in the docs, and was figured out through painful trial + error.
s2n_set_server_name(connection, hostname);
// Turn off randomized side-channel blinding. This feature is useful for highly
// secure servers, but for our simple client it's just annoying.
s2n_connection_set_blinding(connection, S2N_SELF_SERVICE_BLINDING);
#endif
#endif // defined(TLS_WINDOWS) || defined(TLS_S2N)
#ifdef TLS_APPLE
// Used for syncing the packet queue.
pthread_mutex_init(&ctx->q.lock, NULL);
// Turn on TLS (default config).
nw_endpoint_t endpoint = nw_endpoint_create_host(hostname, sport);
nw_parameters_configure_protocol_block_t configure_tls = NW_PARAMETERS_DEFAULT_CONFIGURATION;
nw_parameters_t parameters = nw_parameters_create_secure_tcp(configure_tls, NW_PARAMETERS_DEFAULT_CONFIGURATION);
// Set ipv4.
nw_protocol_stack_t protocol_stack = nw_parameters_copy_default_protocol_stack(parameters);
nw_protocol_options_t ip_options = nw_protocol_stack_copy_internet_protocol(protocol_stack);
nw_ip_options_set_version(ip_options, nw_ip_version_4);
// Create actual connection object.
ctx->connection = nw_connection_create(endpoint, parameters);
nw_retain(ctx->connection);
// Create an async queue for dispatching all of our connection's callbacks/blocks.
ctx->dispatch = dispatch_queue_create("com.tls.internal_queue", DISPATCH_QUEUE_SERIAL);
dispatch_retain(ctx->dispatch);
// Various calls into Network.framework are asynchronous and use a queue to dispatch callbacks (blocks).
nw_connection_set_queue(ctx->connection, ctx->dispatch);
// Get notification of when the connection is ready.
nw_connection_set_state_changed_handler(ctx->connection, ^(nw_connection_state_t state, nw_error_t error) {
if (error) {
int code = nw_error_get_error_code(error);
nw_error_domain_t domain = nw_error_get_error_domain(error);
if (domain == nw_error_domain_tls) {
if (code == errSSLCertExpired) {
ctx->state = TLS_STATE_CERTIFICATE_EXPIRED;
} else if (code == errSSLNegotiation) {
ctx->state = TLS_STATE_NO_MATCHING_ENCRYPTION_ALGORITHMS;
} else if (code == errSSLClientCertRequested) {
ctx->state = TLS_STATE_SERVER_ASKED_FOR_CLIENT_CERTS;
} else if (code == errSSLHostNameMismatch) {
ctx->state = TLS_STATE_BAD_HOSTNAME;
} else if (code == errSSLXCertChainInvalid || code == errSSLPeerUnknownCA) {
ctx->state = TLS_STATE_CANNOT_VERIFY_CA_CHAIN;
} else if (code == errSSLBadCert) {
ctx->state = TLS_STATE_BAD_CERTIFICATE;
} else {
ctx->state = TLS_STATE_UNKNOWN_ERROR;
}
// More codes found at:
// https://opensource.apple.com/source/Security/Security-55471/libsecurity_ssl/lib/SecureTransport.h.auto.html
} else if (domain == nw_error_domain_dns) {
if (code == -65554) { // Could not find what fucking header this code is defined within... *Shakes head at Tim Cook*
ctx->state = TLS_STATE_BAD_HOSTNAME;
} else {
ctx->state = TLS_STATE_UNKNOWN_ERROR;
}
} else if (domain == TLS_STATE_INVALID_SOCKET) {
ctx->state = TLS_STATE_INVALID_SOCKET;
} else {
ctx->state = TLS_STATE_UNKNOWN_ERROR;
}
} else {
if (state == nw_connection_state_ready) {
ctx->state = TLS_STATE_CONNECTED;
} else if (state > nw_connection_state_ready) {
ctx->state = TLS_STATE_DISCONNECTED;
}
}
});
// Asynchronously start the connection.
nw_connection_start(ctx->connection);
ctx->state = TLS_STATE_PENDING;
// Accept incoming packets.
tls_recv(ctx);
nw_release(ip_options);
nw_release(protocol_stack);
nw_release(parameters);
nw_release(endpoint);
#endif
result.id = (unsigned long long)ctx;
return result;
}
TLS_State tls_process(TLS_Connection connection)
{
TLS_Context* ctx = (TLS_Context*)connection.id;
if (ctx->state < 0) {
return ctx->state;
} else if (ctx->state == TLS_STATE_PENDING) {
#if defined(TLS_WINDOWS) || defined(TLS_S2N)
// Wait for TCP to connect.
if (ctx->tcp_connect_pending) {
fd_set sockets_to_check;
FD_ZERO(&sockets_to_check);
FD_SET(ctx->sock, &sockets_to_check);
struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 0;
if (select((int)(ctx->sock + 1), NULL, &sockets_to_check, NULL, &timeout) == 1) {
int opt = -1;
socklen_t len = sizeof(opt);
if (getsockopt(ctx->sock, SOL_SOCKET, SO_ERROR, (char*)(&opt), &len) >= 0 && opt == 0) {
ctx->tcp_connect_pending = 0;
}
}
if (ctx->tcp_connect_pending) {
return ctx->state;
}
}
#endif // defined(TLS_WINDOWS) || defined(TLS_S2N)
#ifdef TLS_WINDOWS
// TLS handshake algorithm.
// 1. Call InitializeSecurityContext.
// The first call creates a security context.
// Subsequent calls update the security context.
// 2. Check InitializeSecurityContext's return value.
// SEC_E_OK - Handshake completed, TLS tunnel ready to go.
// SEC_I_INCOMPLETE_CREDENTIALS - The server asked for client certs (not supported).
// SEC_I_CONTINUE_NEEDED - Success, keep calling InitializeSecurityContext (and send).
// SEC_E_INCOMPLETE_MESSAGE - Success, continue reading data from the server (recv).
// 3. Otherwise an error may have been encountered. Set an error state and return.
// 4. Read data from the server (recv).
// 1. Call InitializeSecurityContext.
if (ctx->first_call || ctx->received) {
SecBuffer inbuffers[2] = { 0 };
inbuffers[0].BufferType = SECBUFFER_TOKEN;
inbuffers[0].pvBuffer = ctx->incoming;
inbuffers[0].cbBuffer = ctx->received;
inbuffers[1].BufferType = SECBUFFER_EMPTY;
SecBuffer outbuffers[1] = { 0 };
outbuffers[0].BufferType = SECBUFFER_TOKEN;
SecBufferDesc indesc = { SECBUFFER_VERSION, TLS_ARRAYSIZE(inbuffers), inbuffers };
SecBufferDesc outdesc = { SECBUFFER_VERSION, TLS_ARRAYSIZE(outbuffers), outbuffers };
DWORD flags = ISC_REQ_USE_SUPPLIED_CREDS | ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_CONFIDENTIALITY | ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT | ISC_REQ_STREAM;
SECURITY_STATUS sec = InitializeSecurityContextA(
&ctx->handle,
ctx->first_call ? NULL : &ctx->context,
ctx->first_call ? (SEC_CHAR*)ctx->hostname : NULL,
flags,
0,
0,
ctx->first_call ? NULL : &indesc,
0,
ctx->first_call ? &ctx->context : NULL,
&outdesc,
&flags,
NULL
);
// After the first call we are supposed to re-use the same context.
ctx->first_call = 0;
// Fetch incoming data.
if (inbuffers[1].BufferType == SECBUFFER_EXTRA) {
TLS_MEMMOVE(ctx->incoming, ctx->incoming + (ctx->received - inbuffers[1].cbBuffer), inbuffers[1].cbBuffer);
ctx->received = inbuffers[1].cbBuffer;
} else if (inbuffers[1].BufferType != SECBUFFER_MISSING) {
ctx->received = 0;
}
// 2. Check InitializeSecurityContext's return value.
if (sec == SEC_E_OK) {
// Successfully completed handshake. TLS tunnel is now operational.
QueryContextAttributes(&ctx->context, SECPKG_ATTR_STREAM_SIZES, &ctx->sizes);
ctx->state = TLS_STATE_CONNECTED;
return ctx->state;
} else if (sec == SEC_I_INCOMPLETE_CREDENTIALS) {
// Client certs are not supported.
ctx->state = TLS_STATE_SERVER_ASKED_FOR_CLIENT_CERTS;
return ctx->state;
} else if (sec == SEC_I_CONTINUE_NEEDED) {
// Continue sending data to the server.
char* buffer = (char*)outbuffers[0].pvBuffer;
int size = outbuffers[0].cbBuffer;
while (size != 0) {
int d = send(ctx->sock, buffer, size, 0);
if (d <= 0) {
break;
}
size -= d;
buffer += d;
}
FreeContextBuffer(outbuffers[0].pvBuffer);
if (size != 0) {
// Somehow failed to send() data to server.
ctx->state = TLS_STATE_UNKNOWN_ERROR;
return ctx->state;
}
} else if (sec != SEC_E_INCOMPLETE_MESSAGE) {
if (sec == SEC_E_CERT_EXPIRED) {
ctx->state = TLS_STATE_CERTIFICATE_EXPIRED;
} else if (sec == SEC_E_WRONG_PRINCIPAL) {
ctx->state = TLS_STATE_BAD_HOSTNAME;
} else if (sec == SEC_E_UNTRUSTED_ROOT) {
ctx->state = TLS_STATE_CANNOT_VERIFY_CA_CHAIN;
} else if (sec == SEC_E_ILLEGAL_MESSAGE || sec == SEC_E_ALGORITHM_MISMATCH) {
ctx->state = TLS_STATE_NO_MATCHING_ENCRYPTION_ALGORITHMS;
} else {
ctx->state = TLS_STATE_UNKNOWN_ERROR;
}
return ctx->state;
} else {
TLS_ASSERT(sec == SEC_E_INCOMPLETE_MESSAGE);
// Need to read more bytes.
}
}
if (ctx->received == sizeof(ctx->incoming)) {
// Server is sending too much data instead of proper handshake?
ctx->state = TLS_STATE_UNKNOWN_ERROR;
return ctx->state;
}
// 4. Read data from the server (recv).
tls_recv(ctx);
#endif // TLS_WINDOWS
#ifdef TLS_APPLE
// Nothing needed here.
#endif // TLS_APPLE
#ifdef TLS_S2N
// s2n wraps a file descriptor (our socket) and performs the entire
// handshake for us. Pretty nice!
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
s2n_errno = S2N_ERR_T_OK;
if (s2n_negotiate(ctx->connection, &blocked) != S2N_SUCCESS) {
s2n_error_type etype = (s2n_error_type)s2n_error_get_type(s2n_errno);
if (etype == S2N_ERR_T_PROTO) {
// For some unknown reason s2n doesn't expose their error constants, like at all.
// So to avoid finding the correct header and including it, we can at least us
// string comparisons, as that's the only way s2n has exposed error codes that aren't
// a nightmare to hookup, and likely won't break as they add new error types.
#define TLS_S2N_ERROR_MATCHES(X) (!TLS_STRCMP(s2n_strerror_name(s2n_errno), #X))
if (TLS_S2N_ERROR_MATCHES(S2N_ERR_CERT_UNTRUSTED) ||
TLS_S2N_ERROR_MATCHES(S2N_ERR_CERT_REVOKED) ||
TLS_S2N_ERROR_MATCHES(S2N_ERR_CERT_TYPE_UNSUPPORTED) ||
TLS_S2N_ERROR_MATCHES(S2N_ERR_CERT_INVALID)) {
ctx->state = TLS_STATE_BAD_CERTIFICATE;
} else if (TLS_S2N_ERROR_MATCHES(S2N_ERR_CERT_EXPIRED)) {
ctx->state = TLS_STATE_CERTIFICATE_EXPIRED;
} else if (TLS_S2N_ERROR_MATCHES(S2N_ERR_NO_APPLICATION_PROTOCOL)) {
ctx->state = TLS_STATE_NO_MATCHING_ENCRYPTION_ALGORITHMS;
}
} else if (etype == S2N_ERR_T_IO) {
ctx->state = TLS_STATE_INVALID_SOCKET;
} else if (etype != S2N_ERR_T_BLOCKED) {
ctx->state = TLS_STATE_UNKNOWN_ERROR;
} else {
// Continue calling s2n_negotiate...
}
} else {
ctx->state = TLS_STATE_CONNECTED;
}
#endif // TLS_S2N
} else if (ctx->state >= 0) {
// Stall if the packet queue is full.
if (ctx->q.count == TLS_PACKET_QUEUE_MAX_ENTRIES) {
// User needs to call tls_read.
return TLS_STATE_PACKET_QUEUE_FILLED;
}
// If any ciphertext data available then try to decrypt it.
#ifdef TLS_WINDOWS
// Read data from the TCP socket.
tls_recv(ctx);
if (ctx->received != 0) {
SecBuffer buffers[4];
TLS_ASSERT(ctx->sizes.cBuffers == TLS_ARRAYSIZE(buffers));
buffers[0].BufferType = SECBUFFER_DATA;
buffers[0].pvBuffer = ctx->incoming;
buffers[0].cbBuffer = ctx->received;
buffers[1].BufferType = SECBUFFER_EMPTY;
buffers[2].BufferType = SECBUFFER_EMPTY;
buffers[3].BufferType = SECBUFFER_EMPTY;
SecBufferDesc desc = { SECBUFFER_VERSION, TLS_ARRAYSIZE(buffers), buffers };
SECURITY_STATUS sec = DecryptMessage(&ctx->context, &desc, 0, NULL);
if (sec == SEC_E_OK) {
// Successfully decrypted some data.
TLS_ASSERT(buffers[0].BufferType == SECBUFFER_STREAM_HEADER);
TLS_ASSERT(buffers[1].BufferType == SECBUFFER_DATA);
TLS_ASSERT(buffers[2].BufferType == SECBUFFER_STREAM_TRAILER);
ctx->decrypted = (char*)buffers[1].pvBuffer;
ctx->available = buffers[1].cbBuffer;
ctx->used = ctx->received - (buffers[3].BufferType == SECBUFFER_EXTRA ? buffers[3].cbBuffer : 0);
} else if (sec == SEC_I_CONTEXT_EXPIRED) {
// Server closed TLS connection (but socket is still open).
ctx->state = TLS_STATE_DISCONNECTED;
return ctx->state;
} else if (sec == SEC_I_RENEGOTIATE) {
// Server wants to renegotiate TLS connection, not implemented here.
ctx->state = TLS_STATE_UNKNOWN_ERROR;
return ctx->state;
} else if (sec != SEC_E_INCOMPLETE_MESSAGE) {
ctx->state = TLS_STATE_UNKNOWN_ERROR;
return ctx->state;
} else {
TLS_ASSERT(sec == SEC_E_INCOMPLETE_MESSAGE);
// More data needs to be read.
}
}
// Copy out a decrypted buffer into an output packet.
if (ctx->decrypted) {
TLS_ASSERT(ctx->decrypted);
int size = ctx->available;