-
Notifications
You must be signed in to change notification settings - Fork 387
/
sslscan.c
6122 lines (5345 loc) · 239 KB
/
sslscan.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
/***************************************************************************
* sslscan - A SSL cipher scanning tool *
* Copyright 2007-2009 by Ian Ventura-Whiting (Fizz) *
* Copyright 2010 by Michael Boman ([email protected]) *
* Copyleft 2010 by Jacob Appelbaum <[email protected]> *
* Copyleft 2013 by rbsec <[email protected]> *
* Copyleft 2014 by Julian Kornberger <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
* In addition, as a special exception, the copyright holders give *
* permission to link the code of portions of this program with the *
* OpenSSL library under certain conditions as described in each *
* individual source file, and distribute linked combinations *
* including the two. *
* You must obey the GNU General Public License in all respects *
* for all of the code used other than OpenSSL. If you modify *
* file(s) with this exception, you may extend this exception to your *
* version of the file(s), but you are not obligated to do so. If you *
* do not wish to do so, delete this exception statement from your *
* version. If you delete this exception statement from all source *
* files in the program, then also delete it here. *
***************************************************************************/
#define _GNU_SOURCE
// Includes...
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdint.h>
#include <winbase.h>
#ifdef _MSC_VER
// For access().
#include <io.h>
// Flag for access() call.
#define R_OK 4
// access() happens to be deprecated, so use the secure version instead.
#define access _access_s
// There is no snprintf(), but _snprintf() instead.
#define snprintf _snprintf
// Calling close() on a socket descriptor instead of closesocket() causes
// a crash!
#define close closesocket
// Visual Studio doesn't have ssize_t...
typedef int ssize_t;
#else
void *memmem(const void *haystack_start, size_t haystack_len, const void *needle, size_t needle_len);
/* Taken from https://sourceforge.net/p/mingw/bugs/_discuss/thread/ec0291f1/93ae/attachment/patchset-wrapped.diff:*/
#define timersub(a, b, result) \
do { \
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
if ((result)->tv_usec < 0) { \
--(result)->tv_sec; \
(result)->tv_usec += 1000000L; \
} \
} while (0)
#ifdef BUILD_32BIT
#include "win32bit-compat.h"
#endif
#endif
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
#else
#include <netdb.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <fcntl.h>
#endif
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <openssl/ec.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/pkcs12.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/ocsp.h>
#ifdef __linux__
#include <arpa/inet.h>
#endif
#ifndef OPENSSL_NO_COMP
#include <openssl/comp.h>
#endif
// If we're not compiling with Visual Studio, include unistd.h. VS
// doesn't have this header.
#ifndef _MSC_VER
#include <unistd.h>
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__)
#include <netinet/in.h>
#endif
/* Format specifier for printing a size_t. */
#ifdef _WIN32
#define SIZE_T_FMT PRIu64
#else
#define SIZE_T_FMT "zu"
#endif
#include "sslscan.h"
#if OPENSSL_VERSION_NUMBER < 0x1010100fL
#error "OpenSSL v1.1.1 or later is required!"
#endif
/* Borrowed from tortls.c to dance with OpenSSL on many platforms, with
* many versions and releases of OpenSSL. */
/** Does the run-time openssl version look like we need
* SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION? */
static int use_unsafe_renegotiation_op = 0;
/** Does the run-time openssl version look like we need
* SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION? */
static int use_unsafe_renegotiation_flag = 0;
/** Does output xml to stdout? */
static int xml_to_stdout = 0;
#if OPENSSL_VERSION_NUMBER < 0x1000100L
unsigned long SSL_CIPHER_get_id(const SSL_CIPHER* cipher) { return cipher->id; }
#endif
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
const SSL_METHOD *TLSv1_3_client_method(void)
{
return TLS_client_method();
}
const SSL_METHOD *TLSv1_3_method(void)
{
return TLS_method();
}
/* Callback set through SSL_set_security_callback() and SSL_CTX_set_security_callback(). Allows all weak algorithms. */
static int security_callback_allow_all(const SSL *s, const SSL_CTX *ctx, int op, int bits, int nid, void *other, void *ex) {
return 1;
}
/* Creates an SSL_CTX using SSL_CTX_new(), sets the security level to 0, and sets the permissive security callback on it. Free with FREE_CTX(). */
SSL_CTX *new_CTX(const SSL_METHOD *method) {
SSL_CTX *ret = SSL_CTX_new(method);
SSL_CTX_set_security_level(ret, 0);
SSL_CTX_set_security_callback(ret, security_callback_allow_all);
SSL_CTX_set_quiet_shutdown(ret, 1);
return ret;
}
/* Creates an SSL object using SSL_new(), sets the security level to 0, and sets the permissive security callback on it. Free with FREE_SSL(). */
SSL *new_SSL(SSL_CTX *ctx) {
SSL *ret = SSL_new(ctx);
SSL_set_security_level(ret, 0);
SSL_set_security_callback(ret, security_callback_allow_all);
return ret;
}
// Adds Ciphers to the Cipher List structure
int populateCipherList(struct sslCheckOptions *options, const SSL_METHOD *sslMethod)
{
int returnCode = true;
struct sslCipher *sslCipherPointer;
int tempInt;
int loop;
// STACK_OF is a sign that you should be using C++ :)
STACK_OF(SSL_CIPHER) *cipherList;
SSL *ssl = NULL;
options->ctx = new_CTX(sslMethod);
if (options->ctx == NULL) {
printf_error("Could not create CTX object.");
return false;
}
SSL_CTX_set_cipher_list(options->ctx, CIPHERSUITE_LIST_ALL);
ssl = new_SSL(options->ctx);
if (ssl == NULL) {
printf_error("Could not create SSL object.");
FREE_CTX(options->ctx);
return false;
}
cipherList = SSL_get_ciphers(ssl);
// Create Cipher Struct Entries...
for (loop = 0; loop < sk_SSL_CIPHER_num(cipherList); loop++)
{
if (options->ciphers == 0)
{
options->ciphers = malloc(sizeof(struct sslCipher));
sslCipherPointer = options->ciphers;
}
else
{
sslCipherPointer = options->ciphers;
while (sslCipherPointer->next != 0)
sslCipherPointer = sslCipherPointer->next;
sslCipherPointer->next = malloc(sizeof(struct sslCipher));
sslCipherPointer = sslCipherPointer->next;
}
// Init
memset(sslCipherPointer, 0, sizeof(struct sslCipher));
// Add cipher information...
sslCipherPointer->sslMethod = sslMethod;
sslCipherPointer->name = SSL_CIPHER_get_name(sk_SSL_CIPHER_value(cipherList, loop));
sslCipherPointer->version = SSL_CIPHER_get_version(sk_SSL_CIPHER_value(cipherList, loop));
SSL_CIPHER_description(sk_SSL_CIPHER_value(cipherList, loop), sslCipherPointer->description, sizeof(sslCipherPointer->description) - 1);
sslCipherPointer->bits = SSL_CIPHER_get_bits(sk_SSL_CIPHER_value(cipherList, loop), &tempInt);
}
FREE_SSL(ssl);
FREE_CTX(options->ctx);
return returnCode;
}
// File Exists
int fileExists(char *fileName)
{
return access(fileName, R_OK) == 0;
}
// Read a line from the input...
void readLine(FILE *input, char *lineFromFile, int maxSize)
{
// Variables...
int stripPointer;
// Read line from file...
fgets(lineFromFile, maxSize, input);
// Clear the end-of-line stuff...
stripPointer = strlen(lineFromFile) -1;
while (stripPointer >= 0 && ((lineFromFile[stripPointer] == '\r') || (lineFromFile[stripPointer] == '\n') || (lineFromFile[stripPointer] == ' ')))
{
lineFromFile[stripPointer] = 0;
stripPointer--;
}
}
int readOrLogAndClose(int fd, void* buffer, size_t len, const struct sslCheckOptions *options)
{
ssize_t n;
if (len < 2)
return 1;
n = recv(fd, buffer, len - 1, 0);
if (n < 0 && errno != 11) {
printf_error("Error reading from %s:%d: %s", options->host, options->port, strerror(errno));
close(fd);
return 0;
} else if (n == 0) {
printf_error("Unexpected EOF reading from %s:%d", options->host, options->port);
close(fd);
return 0;
} else {
((unsigned char *)buffer)[n] = 0;
}
return 1;
}
// Write a null-terminated string to a socket
ssize_t sendString(int sockfd, const char str[])
{
return send(sockfd, str, strlen(str), 0);
}
char *sock_strerror(int err)
{
#ifdef _WIN32
static char msg[255];
msg[0] = '\0';
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), msg, sizeof(msg), NULL) == 0 || msg[0] == '\0')
{
sprintf(msg, "Error code %d", err);
}
return msg;
#else
return strerror(err);
#endif
}
int tcpConnectSocket(int socket, struct sslCheckOptions *options, char *error, int errlen)
{
int status = -1, flags, errn = 0, len;
fd_set rset, wset, eset;
struct timeval tval;
#ifdef _WIN32
#define INPROGRESS WSAEWOULDBLOCK
#define sock_errno WSAGetLastError()
flags = 1;
if ((status = ioctlsocket(socket, FIONBIO, (u_long *)&flags)) != 0)
{
snprintf(error, errlen, "ioctlsocket: %s", sock_strerror(sock_errno));
return status;
}
#else
#define INPROGRESS EINPROGRESS
#define sock_errno errno
if ((flags = fcntl(socket, F_GETFL, 0)) < 0)
{
snprintf(error, errlen, "fcntl getfl: %s", sock_strerror(sock_errno));
return status;
}
if (fcntl(socket, F_SETFL, flags | O_NONBLOCK) < 0)
{
snprintf(error, errlen, "fcntl setfl: %s", sock_strerror(sock_errno));
return status;
}
#endif
// Connect
if (options->h_addrtype == AF_INET)
{
status = connect(socket, (struct sockaddr *)&options->serverAddress, sizeof(options->serverAddress));
}
else // IPv6
{
status = connect(socket, (struct sockaddr *)&options->serverAddress6, sizeof(options->serverAddress6));
}
if (status < 0 && sock_errno != INPROGRESS)
{
snprintf(error, errlen, "connect: %s", sock_strerror(sock_errno));
return status;
}
// connect() completed immediately
if (status == 0)
return status;
FD_ZERO(&rset);
FD_SET(socket, &rset);
wset = eset = rset;
tval.tv_sec = options->connect_timeout;
tval.tv_usec = 0;
if ((status = select(socket + 1, &rset, &wset, &eset, &tval)) == 0)
{
snprintf(error, errlen, "connect: Timed out");
return -1;
}
else if (status < 0)
{
snprintf(error, errlen, "connect: select: %s", sock_strerror(sock_errno));
return status;
}
if (FD_ISSET(socket, &rset) || FD_ISSET(socket, &wset) || FD_ISSET(socket, &eset))
{
len = sizeof(errn);
if (getsockopt(socket, SOL_SOCKET, SO_ERROR, (void *)&errn, (socklen_t *)&len) < 0)
{
snprintf(error, errlen, "connect: getsockopt: %s", sock_strerror(errn));
return -1;
}
}
if (errn)
{
snprintf(error, errlen, "connect: %s", sock_strerror(errn));
return -1;
}
#ifdef _WIN32
flags = 0;
if ((status = ioctlsocket(socket, FIONBIO, (u_long *)&flags)) != NO_ERROR)
{
snprintf(error, errlen, "ioctlsocket: %s", sock_strerror(sock_errno));
return -1;
}
#else
if (fcntl(socket, F_SETFL, flags) < 0)
{
snprintf(error, errlen, "fcntl setfl: %s", sock_strerror(sock_errno));
return -1;
}
#endif
return status;
}
// Create a TCP socket
int tcpConnect(struct sslCheckOptions *options)
{
//Sleep if required
if (options->sleep > 0)
{
SLEEPMS(options->sleep);
}
// Variables...
int socketDescriptor;
int tlsStarted = 0;
char buffer[BUFFERSIZE], errmsg[BUFFERSIZE];
int status;
// Create Socket
if (options->h_addrtype == AF_INET)
{
socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
}
else // IPv6
{
socketDescriptor = socket(AF_INET6, SOCK_STREAM, 0);
}
if(socketDescriptor < 0)
{
printf_error("Could not open a socket.");
return 0;
}
// Set socket timeout
#ifdef _WIN32
// Windows isn't looking for a timeval struct like in UNIX; it wants a timeout in a DWORD represented in milliseconds...
DWORD timeout = (options->timeout.tv_sec * 1000) + (options->timeout.tv_usec / 1000);
setsockopt(socketDescriptor, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
setsockopt(socketDescriptor, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
#else
setsockopt(socketDescriptor, SOL_SOCKET, SO_RCVTIMEO, (char *)&options->timeout, sizeof(struct timeval));
setsockopt(socketDescriptor, SOL_SOCKET, SO_SNDTIMEO, (char *)&options->timeout, sizeof(struct timeval));
#endif
status = tcpConnectSocket(socketDescriptor, options, errmsg, BUFFERSIZE);
if(status < 0)
{
printf_error("Could not open a connection to host %s (%s) on port %d (%s).", options->host, options->addrstr,
options->port, errmsg);
close(socketDescriptor);
return 0;
}
// If STARTTLS is required...
if (options->starttls_smtp == true && tlsStarted == false)
{
tlsStarted = 1;
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
if (strncmp(buffer, "220", 3) != 0)
{
close(socketDescriptor);
printf_error("The host %s on port %d did not appear to be an SMTP service.", options->host, options->port);
return 0;
}
sendString(socketDescriptor, "EHLO example.org\r\n");
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
if (strncmp(buffer, "250", 3) != 0)
{
close(socketDescriptor);
printf_error("The SMTP service on %s port %d did not respond with status 250 to our HELO.", options->host, options->port);
return 0;
}
sendString(socketDescriptor, "STARTTLS\r\n");
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
if (strncmp(buffer, "220", 3) != 0)
{
close(socketDescriptor);
printf_error("The SMTP service on %s port %d did not appear to support STARTTLS.", options->host, options->port);
return 0;
}
}
if (options->starttls_mysql == true && tlsStarted == false)
{
tlsStarted = 1;
// Taken from https://github.com/tetlowgm/sslscan/blob/master/sslscan.c
const char mysqlssl[] = { 0x20, 0x00, 0x00, 0x01, 0x85, 0xae, 0x7f, 0x00,
0x00, 0x00, 0x00, 0x01, 0x21, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
send(socketDescriptor, mysqlssl, sizeof(mysqlssl), 0);
}
// We could use an XML parser but frankly it seems like a security disaster
if (options->starttls_xmpp == true && tlsStarted == false)
{
/* This is so ghetto, you cannot release it! */
char xmpp_setup[1024]; // options->host is 512 bytes long
/* XXX: TODO - options->host isn't always the host you want to test
eg:
talk.google.com actually expects gmail.com, not talk.google.com
jabber.ccc.de expects jabber.ccc.de
It may be useful to provide a commandline switch to provide the
expected hostname.
*/
// Server to server handshake
if (options->xmpp_server)
{
if (snprintf(xmpp_setup, sizeof(xmpp_setup), "<?xml version='1.0' ?>\r\n"
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:server' to='%s' version='1.0'>\r\n", options->host) >= sizeof(xmpp_setup)) {
printf("(internal error: xmpp_setup buffer too small)\n");
abort();
}
}
// Client to server handshake (default)
else
{
if (snprintf(xmpp_setup, sizeof(xmpp_setup), "<?xml version='1.0' ?>\r\n"
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' to='%s' version='1.0'>\r\n", options->host) >= sizeof(xmpp_setup)) {
printf("(internal error: xmpp_setup buffer too small)\n");
abort();
}
}
tlsStarted = 1;
sendString(socketDescriptor, xmpp_setup);
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
printf_verbose("Server reported: %s\nAttempting to STARTTLS\n", buffer);
sendString(socketDescriptor, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>\r\n");
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
/* We're looking for something like:
<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'
If we find the end of the stream features before we find tls, we may
not have STARTTLS support. */
if (strstr(buffer, "urn:ietf:params:xml:ns:xmpp-tls")) {
printf_verbose("It appears that xmpp-tls was detected.\n");
} else if (strstr(buffer, "/stream:features")) {
printf_verbose("It appears that xmpp-tls was not detected.\n");
}
if (options->verbose)
printf("Server reported: %s\n", buffer);
if (strstr(buffer, "<proceed"))
{
printf_verbose("It appears that xmpp-tls is ready for TLS.\n");
}
else
{
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
}
printf_verbose("Server reported: %s\n", buffer);
}
// Setup a POP3 STARTTLS socket
if (options->starttls_pop3 == true && tlsStarted == false)
{
tlsStarted = 1;
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
printf_verbose("Server reported: %s\n", buffer);
sendString(socketDescriptor, "STLS\r\n");
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
// We probably want to confirm that we see something like:
// '+OK Begin SSL/TLS negotiation now.'
// Or
// '+OK Begin TLS negotiation, mate'
if (strstr(buffer, "+OK Begin")) {
printf_verbose("It appears that the POP3 server is ready for TLS.\n");
}
printf_verbose("Server reported: %s\n", buffer);
}
// Setup an IMAP STARTTLS socket
if (options->starttls_imap == true && tlsStarted == false)
{
tlsStarted = 1;
memset(buffer, 0, BUFFERSIZE);
// Fetch the IMAP banner
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
printf_verbose("Server banner: %s\n", buffer);
// Attempt to STARTTLS
sendString(socketDescriptor, ". STARTTLS\r\n");
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
if (strstr(buffer, ". OK") || strstr(buffer, " . OK")){
printf_verbose("STARTLS IMAP setup complete.\nServer reported: %s\n", buffer);
} else{
printf_verbose("STARTLS IMAP setup not complete.\nServer reported: %s\n", buffer);
}
}
if (options->starttls_irc == true && tlsStarted == false)
{
tlsStarted = 1;
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
printf_verbose("Server reported: %s\n", buffer);
// Attempt to STARTTLS
sendString(socketDescriptor, "STARTTLS\r\n");
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
if (strstr(buffer, " 670 ") || strstr(buffer, ":STARTTLS successful")) {
printf_verbose("STARTLS IRC setup complete.\nServer reported %s\n", buffer);
} else {
printf_verbose("STARTLS IRC setup not complete.\nServer reported %s\n", buffer);
}
}
// Setup a LDAP STARTTLS socket
if (options->starttls_ldap == true && tlsStarted == false)
{
tlsStarted = 1;
memset(buffer, 0, BUFFERSIZE);
char starttls[] = {'0', 0x1d, 0x02, 0x01, 0x01, 'w', 0x18, 0x80, 0x16,
'1', '.', '3', '.', '6', '.', '1', '.', '4', '.', '1', '.',
'1', '4', '6', '6', '.', '2', '0', '0', '3', '7'};
char ok[] = "1.3.6.1.4.1.1466.20037";
char unsupported[] = "unsupported extended operation";
// Send TLS
send(socketDescriptor, starttls, sizeof(starttls), 0);
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
if (memmem(buffer, BUFFERSIZE, ok, strlen(ok))) {
printf_verbose("STARTLS LDAP setup complete.\n");
}
else if (strstr(buffer, unsupported)) {
printf_error("STARTLS LDAP connection to %s:%d failed with '%s'.",
options->host, options->port, unsupported);
return 0;
} else {
printf_error("STARTLS LDAP connection to %s:%d failed with unknown error.",
options->host, options->port);
return 0;
}
}
// Setup a FTP STARTTLS socket
if (options->starttls_ftp == true && tlsStarted == false)
{
tlsStarted = 1;
// Fetch the server banner
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
printf_verbose("Server banner: %s\n", buffer);
// Send TLS request
sendString(socketDescriptor, "AUTH TLS\r\n");
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
if (strstr(buffer, "234 AUTH TLS successful")) {
printf_verbose("STARTLS FTP setup complete.\n");
} else {
printf_verbose("STARTLS FTP setup possibly not complete.\n");
}
printf_verbose("Server reported: %s\n", buffer);
}
if (options->starttls_psql == true && tlsStarted == false)
{
unsigned char buffer;
tlsStarted = 1;
// Send SSLRequest packet
send(socketDescriptor, "\x00\x00\x00\x08\x04\xd2\x16\x2f", 8, 0);
// Read reply byte
if (1 != recv(socketDescriptor, &buffer, 1, 0)) {
printf_error("Unexpected EOF reading from %s:%d", options->host, options->port);
return 0;
}
if (buffer != 'S') {
printf_error("Server at %s:%d rejected TLS startup", options->host, options->port);
return 0;
}
}
// Setup an RDP socket with preamble
// Borrowed from https://labs.portcullis.co.uk/tools/ssl-cipher-suite-enum/
if (options->rdp == true && tlsStarted == false)
{
unsigned char buffer[32768];
size_t readlen;
tlsStarted = 1;
// Send RDP preamble
send(socketDescriptor, "\x03\x00\x00\x13\x0e\xe0\x00\x00\x00\x00\x00\x01\x00\x08\x00\x03\x00\x00\x00", 19, 0);
// Read reply header
if (4 != recv(socketDescriptor, buffer, 4, 0)) {
printf_error("Unexpected EOF reading from %s:%d", options->host, options->port);
return 0;
}
// Calculate remaining bytes (and check for overflows)
readlen = ((buffer[2] & 0x7f) << 8) + buffer[3] - 4;
if (readlen > sizeof(buffer)) {
printf_error("Unexpected data from %s:%d", options->host, options->port);
return 0;
}
// Read reply data
if (readlen != recv(socketDescriptor, buffer, readlen, 0)) {
printf_error("Unexpected EOF reading from %s:%d", options->host, options->port);
return 0;
}
}
// Return
return socketDescriptor;
}
// Private Key Password Callback...
static int password_callback(char *buf, int size, int rwflag, void *userdata)
{
strncpy(buf, (char *)userdata, size);
// I don't know the semantics of these arguments, but it looks like this
// could go badly wrong if userdata is too long.
buf[strlen(userdata)] = 0;
return strlen(userdata);
}
// Load client certificates/private keys...
int loadCerts(struct sslCheckOptions *options)
{
// Variables...
int status = 1;
PKCS12 *pk12 = NULL;
FILE *pk12File = NULL;
X509 *cert = NULL;
EVP_PKEY *pkey = NULL;
STACK_OF(X509) *ca = NULL;
// Configure PKey password...
if (options->privateKeyPassword != 0)
{
SSL_CTX_set_default_passwd_cb_userdata(options->ctx, (void *)options->privateKeyPassword);
SSL_CTX_set_default_passwd_cb(options->ctx, password_callback);
}
// Separate Certs and PKey Files...
if ((options->clientCertsFile != 0) && (options->privateKeyFile != 0))
{
// Load Cert...
if (!SSL_CTX_use_certificate_file(options->ctx, options->clientCertsFile, SSL_FILETYPE_PEM))
{
if (!SSL_CTX_use_certificate_file(options->ctx, options->clientCertsFile, SSL_FILETYPE_ASN1))
{
if (!SSL_CTX_use_certificate_chain_file(options->ctx, options->clientCertsFile))
{
printf("%s Could not configure certificate(s).%s\n", COL_RED, RESET);
status = 0;
}
}
}
// Load PKey...
if (status != 0)
{
if (!SSL_CTX_use_PrivateKey_file(options->ctx, options->privateKeyFile, SSL_FILETYPE_PEM))
{
if (!SSL_CTX_use_PrivateKey_file(options->ctx, options->privateKeyFile, SSL_FILETYPE_ASN1))
{
// Why would the more specific functions succeed if the generic functions failed?
// -- I'm guessing that the original author was hopeful? - io
if (!SSL_CTX_use_RSAPrivateKey_file(options->ctx, options->privateKeyFile, SSL_FILETYPE_PEM))
{
if (!SSL_CTX_use_RSAPrivateKey_file(options->ctx, options->privateKeyFile, SSL_FILETYPE_ASN1))
{
printf("%s Could not configure private key.%s\n", COL_RED, RESET);
status = 0;
}
}
}
}
}
}
// PKCS Cert and PKey File...
else if (options->privateKeyFile != 0)
{
pk12File = fopen(options->privateKeyFile, "rb");
if (pk12File != NULL)
{
pk12 = d2i_PKCS12_fp(pk12File, NULL);
if (!pk12)
{
status = 0;
printf("%s Could not read PKCS#12 file.%s\n", COL_RED, RESET);
}
else
{
if (!PKCS12_parse(pk12, options->privateKeyPassword, &pkey, &cert, &ca))
{
status = 0;
printf("%s Error parsing PKCS#12. Are you sure that password was correct?%s\n", COL_RED, RESET);
}
else
{
if (!SSL_CTX_use_certificate(options->ctx, cert))
{
status = 0;
printf("%s Could not configure certificate.%s\n", COL_RED, RESET);
}
if (!SSL_CTX_use_PrivateKey(options->ctx, pkey))
{
status = 0;
printf("%s Could not configure private key.%s\n", COL_RED, RESET);
}
}
PKCS12_free(pk12);
}
fclose(pk12File);
}
else
{
printf("%s Could not open PKCS#12 file.%s\n", COL_RED, RESET);
status = 0;
}
}
// Check Cert/Key...
if (status != 0)
{
if (!SSL_CTX_check_private_key(options->ctx))
{
printf("%s Private key does not match certificate.%s\n", COL_RED, RESET);
return false;
}
else
return true;
}
else
return false;
}
// Test renegotiation
int outputRenegotiation( struct sslCheckOptions *options, struct renegotiationOutput *outputData)
{
printf_xml(" <renegotiation supported=\"%d\" secure=\"%d\" />\n",
outputData->supported, outputData->secure);
if (outputData->secure)
{
printf("%sSecure%s session renegotiation supported\n\n", COL_GREEN, RESET);
}
else if (outputData->supported)
{
printf("%sInsecure%s session renegotiation supported\n\n", COL_RED, RESET);
}
else
{
printf("Session renegotiation %snot supported%s\n\n", COL_GREEN, RESET);
}
return true;
}
struct renegotiationOutput * newRenegotiationOutput( void )
{
struct renegotiationOutput *myRenOut;
myRenOut = calloc(1,sizeof(struct renegotiationOutput));
return( myRenOut );
}
int freeRenegotiationOutput( struct renegotiationOutput *myRenOut )
{
if ( myRenOut != NULL) {
free(myRenOut);
}
return true;
}
void tls_reneg_init(struct sslCheckOptions *options)
{
/* Borrowed from tortls.c to dance with OpenSSL on many platforms, with
* many versions and release of OpenSSL. */
SSL_library_init();
SSL_load_error_strings();
long version = SSLeay();
if (version >= 0x009080c0L && version < 0x009080d0L) {
printf_verbose("OpenSSL %s looks like version 0.9.8l; I will try SSL3_FLAGS to enable renegotiation.\n",
SSLeay_version(SSLEAY_VERSION));
use_unsafe_renegotiation_flag = 1;
use_unsafe_renegotiation_op = 1;
} else if (version >= 0x009080d0L) {
printf_verbose("OpenSSL %s looks like version 0.9.8m or later; "
"I will try SSL_OP to enable renegotiation\n",
SSLeay_version(SSLEAY_VERSION));
use_unsafe_renegotiation_op = 1;
} else if (version < 0x009080c0L) {
printf_verbose("OpenSSL %s [%lx] looks like it's older than "
"0.9.8l, but some vendors have backported 0.9.8l's "
"renegotiation code to earlier versions, and some have "
"backported the code from 0.9.8m or 0.9.8n. I'll set both "
"SSL3_FLAGS and SSL_OP just to be safe.\n",
SSLeay_version(SSLEAY_VERSION), version);
use_unsafe_renegotiation_flag = 1;
use_unsafe_renegotiation_op = 1;
} else {
printf_verbose("OpenSSL %s has version %lx\n",
SSLeay_version(SSLEAY_VERSION), version);
}
#ifdef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
SSL_CTX_set_options(options->ctx,
SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
#endif
}
// Check if the server supports compression
int testCompression(struct sslCheckOptions *options, const SSL_METHOD *sslMethod)
{
// Variables...
int status = true;
int socketDescriptor = 0;
SSL *ssl = NULL;
BIO *cipherConnectionBio;
SSL_SESSION *session;
// Connect to host
socketDescriptor = tcpConnect(options);
if (socketDescriptor != 0)
{
// Setup Context Object...
options->ctx = new_CTX(sslMethod);
tls_reneg_init(options);
if (options->ctx != NULL)
{
if (SSL_CTX_set_cipher_list(options->ctx, CIPHERSUITE_LIST_ALL) != 0)
{
// Load Certs if required...
if ((options->clientCertsFile != 0) || (options->privateKeyFile != 0))
status = loadCerts(options);
if (status == true)
{
// Create SSL object...
ssl = new_SSL(options->ctx);
#if ( OPENSSL_VERSION_NUMBER > 0x009080cfL )
// Make sure we can connect to insecure servers
// OpenSSL is going to change the default at a later date
SSL_set_options(ssl, SSL_OP_LEGACY_SERVER_CONNECT);
#endif
#ifdef SSL_OP_NO_COMPRESSION
// Make sure to clear the no compression flag
SSL_clear_options(ssl, SSL_OP_NO_COMPRESSION);