forked from rbsec/sslscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sslscan.c
3656 lines (3271 loc) · 144 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. *
***************************************************************************/
// 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>
#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;
#endif
#else
#include <netdb.h>
#include <sys/socket.h>
#include <sys/select.h>
#endif
#include <string.h>
#include <sys/stat.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>
// 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
#include "sslscan.h"
#include <mysql.h>
/* 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;
static int mariadb_version_shown= 0;
/** Does the run-time openssl version look like we need
* SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION? */
static int use_unsafe_renegotiation_flag = 0;
// 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 = SSL_CTX_new(sslMethod);
if (options->ctx == NULL) {
printf_error("%sERROR: Could not create CTX object.%s\n", COL_RED, RESET);
return false;
}
SSL_CTX_set_cipher_list(options->ctx, "ALL:COMPLEMENTOFALL");
ssl = SSL_new(options->ctx);
if (ssl == NULL) {
printf_error("%sERROR: Could not create SSL object.%s\n", COL_RED, RESET);
SSL_CTX_free(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);
}
SSL_free(ssl);
SSL_CTX_free(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("%s ERROR: error reading from %s:%d: %s%s\n", COL_RED, options->host, options->port, strerror(errno), RESET);
close(fd);
return 0;
} else if (n == 0) {
printf_error("%s ERROR: unexpected EOF reading from %s:%d%s\n", COL_RED, options->host, options->port, RESET);
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);
}
#define int3store(T,A) do { *(T)= (unsigned char) ((A));\
*(T+1)=(unsigned char) (((unsigned int) (A) >> 8));\
*(T+2)=(unsigned char) (((A) >> 16)); } while (0)
ssize_t sendBuffer(int sockfd, const char *string, size_t length)
{
char *buffer;
size_t bufsize;
ssize_t rc;
bufsize= 4 + length;
buffer= (char *)calloc(1, bufsize);
int3store(buffer, length);
buffer[3]= 1;
memcpy(buffer + 4, string, length);
rc= send(sockfd, buffer, bufsize, 0);
free(buffer);
return rc;
}
// 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];
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("%s ERROR: Could not open a socket.%s\n", COL_RED, RESET);
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));
#else
setsockopt(socketDescriptor, SOL_SOCKET, SO_RCVTIMEO, (char *)&options->timeout,sizeof(struct timeval));
#endif
// Connect
if (options->h_addrtype == AF_INET)
{
status = connect(socketDescriptor, (struct sockaddr *) &options->serverAddress, sizeof(options->serverAddress));
}
else // IPv6
{
status = connect(socketDescriptor, (struct sockaddr *) &options->serverAddress6, sizeof(options->serverAddress6));
}
if(status < 0)
{
printf_error("%sERROR: Could not open a connection to host %s on port %d.%s\n", COL_RED, options->host, options->port, RESET);
close(socketDescriptor);
return 0;
}
/* If we test against a MariaDB server, we need to read/write some unencrypted stuff first */
if (options->testMariaDB)
{
char *p;
unsigned short capabilities;
#if defined(CLIENT_DEFAULT_FLAGS) /* MariaDB Connector/C */
unsigned int client_flags= CLIENT_DEFAULT_FLAGS;
#else
unsigned int client_flags= CLIENT_BASIC_FLAGS;
#endif
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
p= buffer;
p+=4; /* length */
p++; /* protocol */
/* version */
if (!mariadb_version_shown)
{
if (!strncmp(p, "5.5.5-", 6))
p+= 6;
printf("Server version: %s%s\n", COL_GREEN, p);
mariadb_version_shown= 1;
}
p+= strlen(p) + 1;
p+=4;
p+=8;
p++;
capabilities= (*((unsigned short *) (p)));
if (!(capabilities & CLIENT_SSL))
{
close(socketDescriptor);
printf("%s Server doesn't support SSL\n", COL_RED);
return 0;
}
p= buffer;
client_flags |= CLIENT_SSL;
client_flags &= ~CLIENT_COMPRESS;
*((long *) (p))= (long) (client_flags);
p+= 4;
*((long *) (p))= (long) (256 * 256 * 256);
p+= 4;
*p++= 0x21;
memset(p, 0, 23);
p+=23;
/* send buffer to server to initiate ssl handshake */
sendBuffer(socketDescriptor, buffer, p - buffer);
}
// 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("%s ERROR: The host %s on port %d did not appear to be an SMTP service.%s\n", COL_RED, options->host, options->port, RESET);
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("%s ERROR: The SMTP service on %s port %d did not respond with status 250 to our HELO.%s\n", COL_RED, options->host, options->port, RESET);
return 0;
}
sendString(socketDescriptor, "STARTTLS\r\n");
if (!readOrLogAndClose(socketDescriptor, buffer, BUFFERSIZE, options))
return 0;
if (strncmp(buffer, "220", 3) != 0)
{
close(socketDescriptor);
printf("%s ERROR: The SMTP service on %s port %d did not appear to support STARTTLS.%s\n", COL_RED, options->host, options->port, RESET);
return 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 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);
}
// 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("%s ERROR: unexpected EOF reading from %s:%d%s\n", COL_RED, options->host, options->port, RESET);
return 0;
}
// Calculate remaining bytes (and check for overflows)
readlen = ((buffer[2] & 0x7f) << 8) + buffer[3] - 4;
if (readlen > sizeof(buffer)) {
printf_error("%s ERROR: unexpected data from %s:%d%s\n", COL_RED, options->host, options->port, RESET);
return 0;
}
// Read reply data
if (readlen != recv(socketDescriptor, buffer, readlen, 0)) {
printf_error("%s ERROR: unexpected EOF reading from %s:%d%s\n", COL_RED, options->host, options->port, RESET);
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 = SSL_CTX_new(sslMethod);
tls_reneg_init(options);
if (options->ctx != NULL)
{
if (SSL_CTX_set_cipher_list(options->ctx, "ALL:COMPLEMENTOFALL") != 0)
{
// Load Certs if required...
if ((options->clientCertsFile != 0) || (options->privateKeyFile != 0))
status = loadCerts(options);
if (status == true)
{
// Create SSL object...
ssl = SSL_new(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
if (ssl != NULL)
{
// Connect socket and BIO
cipherConnectionBio = BIO_new_socket(socketDescriptor, BIO_NOCLOSE);
// Connect SSL and BIO
SSL_set_bio(ssl, cipherConnectionBio, cipherConnectionBio);
#if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT)
// This enables TLS SNI
SSL_set_tlsext_host_name(ssl, options->host);
#endif
// Connect SSL over socket
SSL_connect(ssl);
session = *SSL_get_session(ssl);
#ifndef OPENSSL_NO_COMP
printf_xml(" <compression supported=\"%d\" />\n",
session.compress_meth);
if (session.compress_meth == 0)
{
printf("Compression %sdisabled%s\n\n", COL_GREEN, RESET);
}
else
{
printf("Compression %senabled%s (CRIME)\n\n", COL_RED, RESET);
}
#endif
// Disconnect SSL over socket
SSL_shutdown(ssl);
// Free SSL object
SSL_free(ssl);
}
else
{
status = false;
printf_error("%s ERROR: Could create SSL object.%s\n", COL_RED, RESET);
}
}
}
else
{
status = false;
printf_error("%s ERROR: Could set cipher.%s\n", COL_RED, RESET);
}
// Free CTX Object
SSL_CTX_free(options->ctx);
}
// Error Creating Context Object
else
{
status = false;
printf_error("%sERROR: Could not create CTX object.%s\n", COL_RED, RESET);
}
// Disconnect from host
close(socketDescriptor);
}
else
{
// Could not connect
printf_error("%sERROR: Could not connect.%s\n", COL_RED, RESET);
status = false;
exit(status);
}
return status;
}
// Check if the server supports renegotiation
int testRenegotiation(struct sslCheckOptions *options, const SSL_METHOD *sslMethod)
{
// Variables...
int cipherStatus;
int status = true;
//int secure = false;
int socketDescriptor = 0;
int res;
SSL *ssl = NULL;
BIO *cipherConnectionBio;
struct renegotiationOutput *renOut = newRenegotiationOutput();
// Connect to host
socketDescriptor = tcpConnect(options);
if (socketDescriptor != 0)
{
// Setup Context Object...
options->ctx = SSL_CTX_new(sslMethod);
tls_reneg_init(options);
if (options->ctx != NULL)
{
if (SSL_CTX_set_cipher_list(options->ctx, "ALL:COMPLEMENTOFALL") != 0)
{
// Load Certs if required...
if ((options->clientCertsFile != 0) || (options->privateKeyFile != 0))
status = loadCerts(options);
if (status == true)
{
// Create SSL object...
ssl = SSL_new(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
if (ssl != NULL)
{
// Connect socket and BIO
cipherConnectionBio = BIO_new_socket(socketDescriptor, BIO_NOCLOSE);
// Connect SSL and BIO
SSL_set_bio(ssl, cipherConnectionBio, cipherConnectionBio);
#if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT)
// This enables TLS SNI
// Based on http://does-not-exist.org/mail-archives/mutt-dev/msg13045.html
// TLS Virtual-hosting requires that the server present the correct
// certificate; to do this, the ServerNameIndication TLS extension is used.
// If TLS is negotiated, and OpenSSL is recent enough that it might have
// support, and support was enabled when OpenSSL was built, mutt supports
// sending the hostname we think we're connecting to, so a server can send
// back the correct certificate.
// NB: finding a server which uses this for IMAP is problematic, so this is
// untested. Please report success or failure! However, this code change
// has worked fine in other projects to which the contributor has added it,
// or HTTP usage.
SSL_set_tlsext_host_name(ssl, options->host);
#endif
// Connect SSL over socket
cipherStatus = SSL_connect(ssl);
/* Yes, we know what we are doing here. No, we do not treat a renegotiation
* as authenticating any earlier-received data. */
if (use_unsafe_renegotiation_flag) {
printf_verbose("use_unsafe_renegotiation_flag\n");
ssl->s3->flags |= SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
}
if (use_unsafe_renegotiation_op) {
printf_verbose("use_unsafe_renegotiation_op\n");
SSL_set_options(ssl,
SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
}
if (cipherStatus == 1)
{
#if ( OPENSSL_VERSION_NUMBER > 0x009080cfL )
// SSL_get_secure_renegotiation_support() appeared first in OpenSSL 0.9.8m
printf_verbose("Attempting secure_renegotiation_support()");
renOut->secure = SSL_get_secure_renegotiation_support(ssl);
if( renOut->secure )
{
// If it supports secure renegotiations,
// it should have renegotiation support in general
renOut->supported = true;
status = true;
}
else
{
#endif
// We can't assume that just because the secure renegotiation
// support failed the server doesn't support insecure renegotiations·
// assume ssl is connected and error free up to here
//setBlocking(ssl); // this is unnecessary if it is already blocking·
printf_verbose("Attempting SSL_renegotiate(ssl)\n");
SSL_renegotiate(ssl); // Ask to renegotiate the connection
// This hangs when an 'encrypted alert' is sent by the server
printf_verbose("Attempting SSL_do_handshake(ssl)\n");
SSL_do_handshake(ssl); // Send renegotiation request to server //TODO :: XXX hanging here
if (ssl->state == SSL_ST_OK)
{
res = SSL_do_handshake(ssl); // Send renegotiation request to server
if( res != 1 )
{
printf_error("\n\nSSL_do_handshake() call failed\n");
}
if (ssl->state == SSL_ST_OK)
{
/* our renegotiation is complete */
renOut->supported = true;
status = true;
} else {
renOut->supported = false;
status = false;
printf_error("\n\nFailed to complete renegotiation\n");
}
} else {
status = false;
renOut->secure = false;
}
#if ( OPENSSL_VERSION_NUMBER > 0x009080cfL )
}
#endif