forked from noxxi/p5-ssl-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze-ssl.pl
885 lines (824 loc) · 25.7 KB
/
analyze-ssl.pl
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
#!/usr/bin/perl
#
# Copyright 2013..2015 Steffen Ullrich <[email protected]>
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
use strict;
use warnings;
use Socket;
use IO::Socket::SSL 1.984;
use IO::Socket::SSL::Utils;
use Getopt::Long qw(:config posix_default bundling);
use Socket qw(AF_INET unpack_sockaddr_in inet_ntoa);
use Data::Dumper;
my ($can_ipv6,$ioclass);
BEGIN {
$can_ipv6 = IO::Socket::SSL->can_ipv6;
if ($can_ipv6) {
if (defined(&Socket::getaddrinfo)
&& defined(&Socket::inet_ntop)
&& defined(&Socket::unpack_sockaddr_in6)
&& defined(&Socket::AF_INET6)) {
Socket->import(qw(AF_INET6 inet_ntop unpack_sockaddr_in6));
} elsif ( eval { require Socket6 }) {
Socket6->import(qw(AF_INET6 inet_ntop unpack_sockaddr_in6));
} else {
$can_ipv6 = undef;
}
}
$ioclass = $can_ipv6 || 'IO::Socket::INET';
}
my $can_ocsp = IO::Socket::SSL->can_ocsp;
my $ocsp_cache = $can_ocsp && IO::Socket::SSL::OCSP_Cache->new;
my %starttls = (
'' => [ 443,undef, 'http' ],
'smtp' => [ 25, \&smtp_starttls, 'smtp' ],
'http_proxy' => [ 443, \&http_connect,'http' ],
'http_upgrade' => [ 80, \&http_upgrade,'http' ],
'imap' => [ 143, \&imap_starttls,'imap' ],
'pop' => [ 110, \&pop_stls,'pop3' ],
'ftp' => [ 21, \&ftp_auth,'ftp' ],
'postgresql' => [ 5432, \&postgresql_init,'default' ],
);
my $verbose = 0;
my $timeout = 10;
my ($stls,$stls_arg);
my $capath;
my $all_ciphers;
my $all_ip = 1;
my $show_chain = 1;
my $dump_chain;
my %conf;
my $max_cipher = 'HIGH:ALL';
my $default_cipher = '';
my $sleep = 0;
GetOptions(
'h|help' => sub { usage() },
'v|verbose:1' => \$verbose,
'd|debug:1' => \$IO::Socket::SSL::DEBUG,
'T|timeout=i' => \$timeout,
'CApath=s' => \$capath,
'show-chain!' => \$show_chain,
'dump-chain' => \$dump_chain,
'all-ciphers' => \$all_ciphers,
'all-ip!' => \$all_ip,
'starttls=s' => sub {
($stls,$stls_arg) = $_[1] =~m{^(\w+)(?::(.*))?$};
usage("invalid starttls $stls") if ! $starttls{$stls||'whatever'};
},
'cert=s' => \$conf{SSL_cert_file},
'key=s' => \$conf{SSL_key_file},
'name=s' => \$conf{SSL_hostname},
'cipher=s' => \$default_cipher,
'max-cipher=s' => \$max_cipher,
'sleep=s' => \$sleep,
) or usage("bad usage");
@ARGV or usage("no hosts given");
my %default_ca =
! $capath ? () :
-d $capath ? ( SSL_ca_path => $capath, SSL_ca_file => '' ) :
-f $capath ? ( SSL_ca_file => $capath, SSL_ca_path => '' ) :
die "no such file or dir: $capath";
my $peer_certificates = IO::Socket::SSL->can('peer_certificates')
|| sub {};
$conf{SSL_verifycn_name} ||= $conf{SSL_hostname} if $conf{SSL_hostname};
if ($conf{SSL_cert_file}) {
$conf{SSL_key_file} ||= $conf{SSL_cert_file};
$conf{SSL_use_cert} = 1;
}
sub usage {
print STDERR "ERROR: @_\n" if @_;
print STDERR <<USAGE;
Analyze SSL connectivity for problems.
Usage: $0 [options] (host|host:port)+
Options:
-h|--help - this screen
-d|--debug level - IO::Socket::SSL/Net::SSLeay debugging
# how to connect
--starttls proto[:arg] - start plain and upgrade to SSL with starttls protocol
(imap,smtp,http_upgrade,http_proxy,pop,ftp,postgresql)
-T|--timeout T - use timeout (default 10)
--sleep X - sleep X seconds between requests to reduce load on server
can also be float like 0.1
# SSL specific settings
--CApath file|dir - use given dir|file instead of system default CA store
--cert cert - use given certificate for client authentication
--key key - use given key for client authentication (default: cert)
--name name - use given name as server name in verification and SNI
instead of host (useful if target is given as IP)
--cipher set - default cipher set to try, default OpenSSL default
--max-cipher set - maximum cipher set to try, default HIGH:ALL.
Some servers or middleboxes have problems with this set
so it can be reduced.
# what to test and show
-v|--verbose level - verbose output
--all-ciphers - find out all supported ciphers
--all-ip - check if all IP for host get same certificates, cipher...
This is the default. Use --no-all-ip to disable.
--show-chain - show certificate chain. This is the default. To disable
use --no-show-chain.
--dump_chain - dump certificate chain, e.g. all certificates as PEM
Examples:
$0 --show-chain --all-ciphers -v3 www.live.com:443
$0 --starttls http_proxy:proxy_host:proxy_port www.live.com:443
$0 --starttls imap mail.gmx.de
USAGE
exit(2);
}
my @tests;
for my $host (@ARGV) {
my ($ip,$port);
$host =~m{^(?:\[(\w\.\-\:+)\]|([\w\.\-]+)):(\w+)$|^([\w\.\-:]+)$}
or die "invalid dst: $host";
$host = $1||$2||$4;
my $st = $starttls{$stls ||''};
$port = $3 || $st->[0] || 443;
if ( $host =~m{:|^[\d\.]+$} ) {
$ip = $host;
$host = undef;
}
push @tests, [ $host||$ip,$port,$conf{SSL_hostname}||$host,$st->[1],$st->[2] || 'default' ];
}
for my $test (@tests) {
my ($host,$port,$name,$stls_sub,$scheme) = @$test;
my @ip;
if (defined &Socket::getaddrinfo) {
my ($err,@res) = Socket::getaddrinfo($host,$port);
for(@res) {
push @ip,_addr2ip($_->{addr},$_->{family});
}
} elsif (defined &Socket6::getaddrinfo) {
my @res = Socket6::getaddrinfo($host,$port);
while (@res>=5) {
my ($family,$addr) = (splice(@res,0,5))[0,3];
push @ip,_addr2ip($addr,$family);
}
} else {
my $ip = Socket::inet_aton($host);
push @ip, $ip if $ip;
}
if (!@ip) {
warn "Found no IP address for $host\n";
next;
}
if (@ip>1) {
my (%have,@ip4,@ip6);
for(@ip) {
if ($have{$_}++) {
next;
} elsif (m{:}) {
push @ip6,$_
} else {
push @ip4,$_
}
}
# Prefer IPv4 even if host preference is different
@ip = (@ip4,@ip6);
}
VERBOSE(1,"checking host=$host(@ip) port=$port".
($stls ? " starttls=$stls":""));
my @problems;
my $tcp_connect = sub {
my $use_ip = shift;
my $tries = 1;
TRY_IP:
my ($cl,$error);
my %ioargs = (
PeerAddr => $use_ip || $ip[0],
PeerPort => $port,
Timeout => $timeout,
);
for(1..$tries) {
if ($stls_sub) {
last if $cl = eval { $stls_sub->(\%ioargs,$stls_arg) };
$error = $@ || 'starttls error';
$cl = undef;
} elsif ( $cl = $ioclass->new(%ioargs)) {
last;
} else {
$error = "tcp connect: $!";
}
}
return $cl if $cl;
$error ||= "unknown error";
die $error if $use_ip; # no retry
# retry with next IP
my $failed = shift(@ip);
chomp($error);
push @problems, "failed tcp connect to $failed: $error";
if (@ip) {
VERBOSE(1,"$failed failed permanently '$error', trying next");
goto TRY_IP;
}
VERBOSE(1,"$failed failed permanently '$error', no more IP to try");
die $error."\n";
};
my @handshakes;
# basic connects without verification or any TLS extensions (OCSP)
# find out usable version and ciphers. Because some hosts (like cloudflare)
# behave differently if SNI is used we try to use it and only fall back if
# it fails.
my ($version,$cipher,$good_conf);
my $sni = $name;
my $try_sslversion = sub {
my $v = shift;
my (@protocols,@err);
for my $ciphers ( $default_cipher,$max_cipher ) {
my $cl = &$tcp_connect;
if ( _startssl($cl,
%conf,
SSL_version => $v,
SSL_verify_mode => 0,
SSL_hostname => $sni,
SSL_cipher_list => $ciphers,
)) {
$version = $cl->get_sslversion();
$cipher = $cl->get_cipher();
if (!@protocols) {
push @protocols, ($version, $cipher);
} elsif ($protocols[-1] ne $cipher) {
push @protocols, $cipher;
}
$good_conf ||= {
%conf,
SSL_version => $v,
SSL_hostname => $sni,
SSL_cipher_list => $ciphers
};
VERBOSE(2,"version $v no verification, ciphers=$ciphers -> $version,$cipher");
} else {
VERBOSE(2,"version $v, no verification, ciphers=$ciphers -> FAIL! $SSL_ERROR");
push @err, $SSL_ERROR if ! @err || $err[-1] ne $SSL_ERROR;
}
}
return (\@protocols,\@err);
};
my $use_version;
my $best_version;
TRY_PROTOCOLS:
for(
# most compatible handshake - should better be supported by all
'SSLv23',
# version specific handshakes - some hosts fail instead of downgrading
defined &Net::SSLeay::CTX_tlsv1_2_new ? ('TLSv1_2'):(),
defined &Net::SSLeay::CTX_tlsv1_1_new ? ('TLSv1_1'):(),
defined &Net::SSLeay::CTX_tlsv1_new ? ('TLSv1') :(),
defined &Net::SSLeay::CTX_v3_new ? ('SSLv3') :(),
) {
my ($protocols,$err) = $try_sslversion->($_);
if (@$protocols) {
$use_version ||= $_;
$best_version ||= $protocols->[0];
push @handshakes, [ $_, @$protocols ];
} else {
push @handshakes, [ $_,\"@$err" ];
}
}
if ($best_version) {
VERBOSE(1,"successful connect with $best_version, cipher=$cipher, sni="
. ($sni||'<undef>')." and no other TLS extensions");
} elsif ($sni) {
$sni = '';
# retry without SNI
goto TRY_PROTOCOLS;
} else {
die "$host failed basic SSL connect: $SSL_ERROR\n";
}
my $sni_status;
my $need_sni;
if (!$sni) {
if ($version =~m{^TLS}) {
VERBOSE(1,"SNI FAIL!");
push @problems, "using SNI (default)";
$sni_status = 'FAIL';
}
} else {
VERBOSE(1,"SNI success");
$sni_status = 'ok';
# check if it works without SNI
my $cl = &$tcp_connect;
my $fail;
if (!_startssl($cl,
%$good_conf,
SSL_hostname => '',
SSL_verifycn_name => $conf{SSL_hostname}||$host,
SSL_verify_callback => sub {
my ($ok) = @_;
$fail++ if ! $ok;
return 1;
}
)) {
VERBOSE(1,"failed without SNI: $SSL_ERROR");
$sni_status = "SSL upgrade fails without SNI";
$need_sni = 1;
}
if ($fail) {
VERBOSE(1,"certificate verify failed without SNI");
$sni_status = "certificate verify fails without SNI";
}
}
my $ssl_upgrade_get_chain = sub {
my ($cl,%conf) = @_;
my (%verify_chain,@chain,@problems,$fail);
my $chain_failed;
if ( _startssl($cl, %conf,
SSL_verifycn_scheme => 'none',
SSL_verify_callback => sub {
my ($valid,$store,$str,$err,$cert,$depth) = @_;
$chain_failed = 1 if ! $valid;
# Since this only a temporary reference we should convert it
# directly to PEM.
my ($subject,$bits);
$subject = Net::SSLeay::X509_NAME_oneline(
Net::SSLeay::X509_get_subject_name($cert));
if (!$depth) {
my @san = $cl->peer_certificate('subjectAltNames');
for( my $i=0;$i<@san;$i++) {
$san[$i] = 'DNS' if $san[$i] == 2;
$san[$i] .= ":".splice(@san,$i+1,1);
}
$subject .= " SAN=".join(",",@san) if @san;
}
if (my $pkey = Net::SSLeay::X509_get_pubkey($cert)) {
$bits = eval { Net::SSLeay::EVP_PKEY_bits($pkey) };
Net::SSLeay::EVP_PKEY_free($pkey);
}
my $pem = PEM_cert2string($cert);
$verify_chain{$pem} = [
$bits||'???',
$subject,
join('|', grep { $_ } @{ CERT_asHash($cert)->{ocsp_uri} || []}),
$pem,
$depth,
'-'
];
return 1;
},
)) {
for my $cert ( $peer_certificates->($cl) ) {
my ($subject,$bits);
$subject = Net::SSLeay::X509_NAME_oneline(
Net::SSLeay::X509_get_subject_name($cert));
if ( !@chain) {
my @san = $cl->peer_certificate('subjectAltNames');
for( my $i=0;$i<@san;$i++) {
$san[$i] = 'DNS' if $san[$i] == 2;
$san[$i] .= ":".splice(@san,$i+1,1);
}
$subject .= " SAN=".join(",",@san) if @san;
}
if (my $pkey = Net::SSLeay::X509_get_pubkey($cert)) {
$bits = eval { Net::SSLeay::EVP_PKEY_bits($pkey) };
Net::SSLeay::EVP_PKEY_free($pkey);
}
my $pem = PEM_cert2string($cert);
my $vc = delete $verify_chain{$pem};
if (!$vc) {
push @problems, "server sent unused chain certificate ".
"'$subject'";
}
push @chain,[
$bits||'???',
$subject,
join('|', grep { $_ } @{ CERT_asHash($cert)->{ocsp_uri} || []}),
$pem,
$vc ? $vc->[4] : '-', # depth
$#chain+1,
],
}
for (sort { $a->[4] <=> $b->[4] } values %verify_chain) {
push @chain,$_;
}
if ($chain_failed) {
$fail = "validation of certificate chain failed";
} elsif (!$cl->verify_hostname($name,$scheme)) {
$fail = "validation of hostname failed";
}
} else {
$fail = $SSL_ERROR || 'unknown handshake error';
}
return (@chain ? \@chain : undef,\@problems,$fail);
};
# get chain info
my (@cert_chain,@cert_chain_nosni);
if ($show_chain || $dump_chain) {
for(
[ $good_conf, \@cert_chain ],
( $need_sni || ! $good_conf->{SSL_hostname}) ? ()
# cloudflare has different cipher list without SNI, so don't
# enforce the existing one
: ([ { %$good_conf, SSL_cipher_list => $default_cipher || undef, SSL_hostname => '' }, \@cert_chain_nosni ])
) {
my ($conf,$chain) = @$_;
my $cl = &$tcp_connect;
my ($ch,$p) = $cl ? $ssl_upgrade_get_chain->($cl,%$conf) : ();
push @problems,@$p;
if ($ch) {
@$chain = @$ch;
} else {
die "failed to connect with previously successful config: $SSL_ERROR";
}
}
# if same chain ignore nosni
if (@cert_chain_nosni and
@cert_chain_nosni == @cert_chain and
! grep { $cert_chain_nosni[$_][3] ne $cert_chain[$_][3] } (0..$#cert_chain_nosni)) {
VERBOSE(2,"same certificate chain in without SNI");
@cert_chain_nosni = ();
}
}
# check verification against given/builtin CA w/o OCSP
my $verify_status;
my $cl = &$tcp_connect;
if ( _startssl($cl, %$good_conf,
SSL_verify_mode => SSL_VERIFY_PEER,
SSL_ocsp_mode => SSL_OCSP_NO_STAPLE,
SSL_verifycn_scheme => 'none',
%default_ca
)) {
%conf = ( %$good_conf, SSL_verify_mode => SSL_VERIFY_PEER, %default_ca );
if (!$cl->peer_certificate) {
VERBOSE(1,"no peer certificate (anonymous authentication)");
%conf = %$good_conf;
$verify_status = 'anon';
} elsif ( $cl->verify_hostname( $name,$scheme )) {
VERBOSE(1,"certificate verify success");
$verify_status = 'ok';
$verify_status .= " (needs SNI)" if $sni_status =~ /fails without/;
%conf = %$good_conf = ( %conf,
SSL_verifycn_scheme => $scheme,
SSL_verifycn_name => $name,
);
} else {
my @san = $cl->peer_certificate('subjectAltNames');
for( my $i=0;$i<@san;$i++) {
$san[$i] = 'DNS' if $san[$i] == 2;
$san[$i] .= ":".splice(@san,$i+1,1);
}
VERBOSE(1,"certificate verify - name does not match:".
" subject=".$cl->peer_certificate('subject').
" SAN=".join(",",@san)
);
$verify_status = 'name-mismatch';
%conf = %$good_conf = ( %conf, SSL_verifycn_scheme => 'none');
}
} else {
VERBOSE(1,"certificate verify FAIL!");
$verify_status = "FAIL: $SSL_ERROR";
push @problems, "using certificate verification (default) -> $SSL_ERROR";
}
# check with OCSP stapling
my $ocsp_staple;
if ( $can_ocsp && $verify_status eq 'ok' ) {
my $cl = &$tcp_connect;
$conf{SSL_ocsp_cache} = $ocsp_cache;
if ( _startssl($cl, %conf)) {
if ( ${*$cl}{_SSL_ocsp_verify} ) {
$ocsp_staple = 'got stapled response',
} else {
$ocsp_staple = 'no stapled response',
}
VERBOSE(1,"OCSP stapling: $ocsp_staple");
} else {
$ocsp_staple = "FAIL: $SSL_ERROR";
$conf{SSL_ocsp_mode} = SSL_OCSP_NO_STAPLE;
VERBOSE(1,"access with OCSP stapling FAIL!");
push @problems, "using OCSP stapling (default) -> $SSL_ERROR";
}
}
my $ocsp_status;
if ( $can_ocsp && $verify_status eq 'ok' ) {
my $cl = &$tcp_connect;
$conf{SSL_ocsp_mode} |= SSL_OCSP_FULL_CHAIN;
if ( ! _startssl($cl, %conf)) {
die sprintf("failed with SSL_ocsp_mode=%b, even though it succeeded with default mode",
$conf{SSL_ocsp_mode});
}
my $ocsp_resolver = $cl->ocsp_resolver;
my %todo = $ocsp_resolver->requests;
while (my ($uri,$req) = each %todo) {
VERBOSE(3,"need to send %d bytes OCSP request to %s",length($req),$uri);
}
my $errors = $ocsp_resolver->resolve_blocking();
die "resolver not finished " if ! defined $errors;
if ( ! $errors ) {
VERBOSE(1,"all certificates verified");
$ocsp_status = "good";
} else {
VERBOSE(1,"failed to verify certicates: $errors");
$ocsp_status = "FAIL: $errors";
}
if (my $soft_error = $ocsp_resolver->soft_error) {
$ocsp_status .= " (soft error: $soft_error)"
}
}
# Check if all IP return the same cipher, version and certificates
my %all_ip_diff;
my %cert_chain_alternatives;
if ($all_ip and @ip>1) {
my ($cipher,$protocol,@chain,$result);
for my $ip (@ip) {
my $cl = eval { $tcp_connect->($ip) };
if (!$cl) {
VERBOSE(1,"failed tcp connect to IP $ip: $@");
push @problems, "failed tcp connect to IP $ip: $@";
next;
}
my ($ch,$p,$res) = $ssl_upgrade_get_chain->($cl,%conf);
$res ||= 'success';
if (!$ch) {
VERBOSE(1,"failed SSL upgrade on IP $ip");
push @problems, "failed SSL upgrade on IP $ip";
next;
}
if (!@chain) {
# baseline
$result = $res;
@chain = @$ch;
$cipher = $cl->get_cipher;
$protocol = $cl->get_sslversion;
next;
}
my @diff;
push @diff, "result: $res vs. $result" if $res ne $result;
push @diff, "cipher: ".($cl->get_cipher)." vs. $cipher"
if $cipher ne $cl->get_cipher;
push @diff, "protocol: ".($cl->get_sslversion)." vs. $protocol"
if $protocol ne $cl->get_sslversion;
if (Dumper(\@chain) ne Dumper($ch)) {
my $alt = $cert_chain_alternatives{ Dumper($ch) } ||= [ $ch ];
push @$alt,$ip;
my @ch0 = @chain;
my @ch1 = @$ch;
for(my $i=0;1;$i++) {
my $ch0 = shift(@ch0);
my $ch1 = shift(@ch1);
last if ! $ch0 && !$ch1;
if (!$ch0) {
push @diff, "certificate #$i is additional - $ch1->[1]";
} elsif (!$ch1) {
push @diff, "certificate #$i is missing - $ch0->[1]";
} elsif ($ch0->[3] ne $ch1->[3]) {
push @diff, "certificate #$i differs";
}
}
}
if (@diff) {
VERBOSE(1,"different results for SSL upgrade on $ip than on $ip[0]");
my $alt = $all_ip_diff{ Dumper(\@diff) } ||= [ \@diff ];
push @$alt,$ip;
} else {
VERBOSE(1,"same results for SSL upgrade on $ip compared to $ip[0]");
}
}
}
# check out all supported ciphers
my @ciphers;
{
my $c = "$max_cipher:eNULL";
while ($all_ciphers || @ciphers<2 ) {
my $cl = &$tcp_connect;
if ( _startssl($cl,
%conf,
SSL_verify_mode => 0,
SSL_version => $conf{SSL_version},
SSL_cipher_list => $c,
)) {
push @ciphers, [ $cl->get_sslversion, $cl->get_cipher ];
$c .= ":!".$ciphers[-1][1];
VERBOSE(2,"connect with version %s cipher %s",
@{$ciphers[-1]});
} else {
VERBOSE(3,"handshake failed with $c: $SSL_ERROR");
last;
}
}
}
# try to detect if the server accepts our cipher order by trying two
# ciphers in different order
my $server_cipher_order;
if (@ciphers>=2) {
my %used_cipher;
for( "$ciphers[0][1]:$ciphers[1][1]:$max_cipher","$ciphers[1][1]:$ciphers[0][1]:$max_cipher" ) {
my $cl = &$tcp_connect;
if ( _startssl($cl,
%conf,
SSL_version => $use_version,
SSL_verify_mode => 0,
SSL_cipher_list => $_,
)) {
VERBOSE(3,"tried with cipher list '$_' -> ".$cl->get_cipher);
$used_cipher{$cl->get_cipher}++;
} else {
warn "failed to SSL handshake with SSL_cipher_list=$_: $SSL_ERROR";
}
}
if (keys(%used_cipher) == 2) {
VERBOSE(2,"client decides cipher order");
$server_cipher_order = 0;
} elsif ( (values(%used_cipher))[0] == 2 ) {
VERBOSE(2,"server decides cipher order");
$server_cipher_order = 1;
}
}
# summary
print "-- $host port $port".($stls? " starttls $stls":"")."\n";
print " ! $_\n" for(@problems);
print " * maximum SSL version : $best_version ($use_version)\n";
print " * supported SSL versions with handshake used and preferred cipher(s):\n";
printf " * %-9s %-9s %s\n",qw(handshake protocols ciphers);
for(@handshakes) {
printf(" * %-9s %-9s %s\n",
$_->[0],
ref($_->[1])
? ("FAILED: ${$_->[1]}","")
: ($_->[1], join(" ",@{$_}[2..$#$_]))
);
}
print " * cipher order by : ".(
! defined $server_cipher_order ? "unknown\n" :
$server_cipher_order ? "server\n" : "client\n"
);
print " * SNI supported : $sni_status\n" if $sni_status;
print " * certificate verified : $verify_status\n";
for(sort values %all_ip_diff) {
my ($diff,@ip) = @$_;
print " * different results on @ip\n";
print " * $_\n" for(@$diff);
}
if ($show_chain) {
print " * chain on $ip[0]\n";
for(my $i=0;$i<@cert_chain;$i++) {
my $c = $cert_chain[$i];
print " * [$c->[5]/$c->[4]] bits=$c->[0], ocsp_uri=$c->[2], $c->[1]\n"
}
if (@cert_chain_nosni) {
print " * chain on $ip[0] without SNI\n";
for(my $i=0;$i<@cert_chain_nosni;$i++) {
my $c = $cert_chain_nosni[$i];
print " * [$c->[5]/$c->[4]] bits=$c->[0], ocsp_uri=$c->[2], $c->[1]\n"
}
}
for (sort values %cert_chain_alternatives) {
my ($chain,@ip) = @$_;
print " * chain on @ip\n";
for(my $i=0;$i<@$chain;$i++) {
my $c = $chain->[$i];
print " * [$c->[5]/$c->[4]] bits=$c->[0], ocsp_uri=$c->[2], $c->[1]\n"
}
}
}
print " * OCSP stapling : $ocsp_staple\n" if $ocsp_staple;
print " * OCSP status : $ocsp_status\n" if $ocsp_status;
if ($all_ciphers) {
print " * supported ciphers with $use_version handshake\n";
for(@ciphers) {
printf " * %6s %s\n",@$_;
}
}
if ($dump_chain) {
print "---------------------------------------------------------------\n";
for(my $i=0;$i<@cert_chain;$i++) {
my $c = $cert_chain[$i];
print "# $c->[1]\n$c->[3]\n";
}
}
}
sub smtp_starttls {
my $cl = $ioclass->new(%{shift()}) or die "tcp connect: $!";
my $last_status_line = qr/((\d)\d\d(?:\s.*)?)/;
my ($line,$code) = _readlines($cl,$last_status_line);
$code == 2 or die "server denies access: $line\n";
print $cl "EHLO example.com\r\n";
($line,$code) = _readlines($cl,$last_status_line);
$code == 2 or die "server did not accept EHLO: $line\n";
print $cl "STARTTLS\r\n";
($line,$code) = _readlines($cl,$last_status_line);
$code == 2 or die "server did not accept STARTTLS: $line\n";
VERBOSE(3,"...reply to starttls: $line");
return $cl;
}
sub imap_starttls {
my $cl = $ioclass->new(%{shift()}) or die "tcp connect: $!";
<$cl>; # welcome
print $cl "abc STARTTLS\r\n";
while (<$cl>) {
m{^abc (OK)?} or next;
$1 or die "STARTTLS failed: $_";
s{\r?\n$}{};
VERBOSE(3,"...starttls: $_");
return $cl;
}
die "starttls failed";
}
sub pop_stls {
my $cl = $ioclass->new(%{shift()}) or die "tcp connect: $!";
<$cl>; # welcome
print $cl "STLS\r\n";
my $reply = <$cl>;
die "STLS failed: $reply" if $reply !~m{^\+OK};
$reply =~s{\r?\n}{};
VERBOSE(3,"...stls $reply");
return $cl;
}
sub http_connect {
my ($ioargs,$proxy) = @_;
$proxy or die "no proxy host:port given";
$proxy =~m{^(?:\[(.+)\]|([^:]+)):(\w+)$} or die "invalid dst: $proxy";
my $cl = $ioclass->new( %$ioargs,
PeerAddr => $1||$2,
PeerPort => $3,
) or die "tcp connect: $!";
print $cl "CONNECT $ioargs->{PeerAddr}:$ioargs->{PeerPort} HTTP/1.0\r\n\r\n";
my $hdr = _readlines($cl,qr/\r?\n/);
$hdr =~m{\A(HTTP/1\.[01]\s+(\d\d\d)[^\r\n]*)};
die "CONNECT failed: $1" if $2 != 200;
VERBOSE(3,"...connect request: $1");
return $cl;
}
sub http_upgrade {
my ($ioargs,$arg) = @_;
my $hostname = $ioargs->{PeerAddr};
my $cl = $ioclass->new(%$ioargs) or die "tcp connect: $!";
my $rq;
if ( $arg && $arg =~m{^get(?:=(\S+))?}i ) {
my $path = $1 || '/';
$rq = "GET $path HTTP/1.1\r\n".
"Host: $hostname\r\n".
"Upgrade: TLS/1.0\r\n".
"Connection: Upgrade\r\n".
"\r\n";
} else {
my $path = $arg && $arg =~m{^options=(\S+)}i
? $1:'*';
$rq = "OPTIONS $path HTTP/1.1\r\n".
"Host: $hostname\r\n".
"Upgrade: TLS/1.0\r\n".
"Connection: Upgrade\r\n".
"\r\n";
}
print $cl $rq;
my $hdr = _readlines($cl,qr/\r?\n/);
$hdr =~m{\A(HTTP/1\.[01]\s+(\d\d\d)[^\r\n]*)};
die "upgrade not accepted, code=$2 (expect 101): $1" if $2 != 101;
VERBOSE(3,"...tls upgrade request: $1");
return $cl;
}
sub ftp_auth {
my $cl = $ioclass->new(%{shift()}) or die "tcp connect: $!";
my $last_status_line = qr/((\d)\d\d(?:\s.*)?)/;
my ($line,$code) = _readlines($cl,$last_status_line);
die "server denies access: $line\n" if $code != 2;
print $cl "AUTH TLS\r\n";
($line,$code) = _readlines($cl,$last_status_line);
die "AUTH TLS denied: $line\n" if $code != 2;
VERBOSE(3,"...ftp auth: $line");
return $cl;
}
sub postgresql_init {
my $cl = $ioclass->new(%{shift()}) or die "tcp connect: $!";
# magic header to initiate SSL:
# http://www.postgresql.org/docs/devel/static/protocol-message-formats.html
print $cl pack("NN",8,80877103);
read($cl, my $buf,1 ) or die "did not get response from postgresql";
$buf eq 'S' or die "postgresql does not support SSL (response=$buf)";
VERBOSE(3,"...postgresql supports SSL: $buf");
return $cl;
}
sub _readlines {
my ($cl,$stoprx) = @_;
my $buf = '';
while (<$cl>) {
$buf .= $_;
return $buf if ! $stoprx;
next if ! m{\A$stoprx\Z};
return ( m{\A$stoprx\Z},$buf );
}
die "eof" if $buf eq '';
die "unexpected response: $buf";
}
sub _addr2ip {
my ($addr,$family) = @_;
if ($family == AF_INET) {
(undef,$addr) = unpack_sockaddr_in($addr);
return inet_ntoa($addr);
} elsif (!$can_ipv6) {
return ();
} else {
(undef,$addr) = unpack_sockaddr_in6($addr);
return inet_ntop(AF_INET6(),$addr);
}
}
sub _startssl {
select(undef,undef,undef,$sleep) if $sleep;
#VERBOSE(4,"start_SSL(@_)");
IO::Socket::SSL->start_SSL(@_)
}
sub VERBOSE {
my $level = shift;
$verbose>=$level || return;
my $msg = shift;
$msg = sprintf($msg,@_) if @_;
my $prefix = $level == 1 ? '+ ' : $level == 2 ? '* ' : "<$level> ";
print STDERR "$prefix$msg\n";
}