| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Socket; |
| 6 |
use WgetFeature qw(https); |
| 7 |
use SSLTest; |
| 8 |
|
| 9 |
############################################################################### |
| 10 |
|
| 11 |
# TODO: regenerate the certs with endless lifetime |
| 12 |
exit 77; |
| 13 |
|
| 14 |
# code, msg, headers, content |
| 15 |
my %urls = ( |
| 16 |
'/somefile.txt' => { |
| 17 |
code => "200", |
| 18 |
msg => "Dontcare", |
| 19 |
headers => { |
| 20 |
"Content-type" => "text/plain", |
| 21 |
}, |
| 22 |
content => "blabla", |
| 23 |
}, |
| 24 |
); |
| 25 |
|
| 26 |
# Skip the test if openssl is not available |
| 27 |
my $ossl = `openssl version`; |
| 28 |
unless ($ossl =~ m/OpenSSL 1/) |
| 29 |
{ |
| 30 |
exit 77; |
| 31 |
} |
| 32 |
|
| 33 |
my $srcdir; |
| 34 |
if (@ARGV) { |
| 35 |
$srcdir = shift @ARGV; |
| 36 |
} elsif (defined $ENV{srcdir}) { |
| 37 |
$srcdir = $ENV{srcdir}; |
| 38 |
} |
| 39 |
$srcdir = Cwd::abs_path("$srcdir"); |
| 40 |
|
| 41 |
# HOSTALIASES env variable allows us to create hosts file alias. |
| 42 |
my $testhostname = "WgetTestingServer"; |
| 43 |
$ENV{'HOSTALIASES'} = "$srcdir/certs/wgethosts"; |
| 44 |
|
| 45 |
my $addr = gethostbyname($testhostname); |
| 46 |
unless ($addr) |
| 47 |
{ |
| 48 |
warn "Failed to resolve $testhostname, using $srcdir/certs/wgethosts\n"; |
| 49 |
exit 77; |
| 50 |
} |
| 51 |
unless (inet_ntoa($addr) =~ "127.0.0.1") |
| 52 |
{ |
| 53 |
warn "Unexpected IP for localhost: ".inet_ntoa($addr)."\n"; |
| 54 |
exit 77; |
| 55 |
} |
| 56 |
|
| 57 |
# Use Intermediate CA |
| 58 |
my $caconf = "$srcdir/certs/rootca.conf"; |
| 59 |
my $icrtfile = "$srcdir/certs/interca.crt"; |
| 60 |
my $ikeyfile = "$srcdir/certs/interca.key"; |
| 61 |
|
| 62 |
my $icacheck=`(openssl x509 -noout -modulus -in $icrtfile | openssl md5 ; |
| 63 |
openssl rsa -noout -modulus -in $ikeyfile | openssl md5) | |
| 64 |
uniq | wc -l`; |
| 65 |
# Check if certificate and key are correct. |
| 66 |
unless(-e $icrtfile && -e $ikeyfile && $icacheck == 1) |
| 67 |
{ |
| 68 |
exit 77; # skip |
| 69 |
} |
| 70 |
|
| 71 |
# User certificate using intermediate CA |
| 72 |
my $usrcrt = "$srcdir/certs/user.crt"; |
| 73 |
my $usrkey = "$srcdir/certs/user.key"; |
| 74 |
|
| 75 |
my $usrcheck=`(openssl x509 -noout -modulus -in $usrcrt | openssl md5 ; |
| 76 |
openssl rsa -noout -modulus -in $usrkey | openssl md5) | |
| 77 |
uniq | wc -l`; |
| 78 |
# Check if certificate and key are made correctly. |
| 79 |
unless(-e $usrcrt && -e $ikeyfile && $usrcheck == 1) |
| 80 |
{ |
| 81 |
exit 77; # skip |
| 82 |
} |
| 83 |
|
| 84 |
# Try Wget using SSL using certificate signed by intermediate CA. Expect error. |
| 85 |
my $port = 30443; |
| 86 |
my $cmdline = $WgetTest::WGETPATH . " --ca-certificate=$srcdir/certs/". |
| 87 |
"test-ca-cert.pem https://$testhostname:$port/somefile.txt"; |
| 88 |
my $expected_error_code = 5; |
| 89 |
my %existing_files = ( |
| 90 |
); |
| 91 |
|
| 92 |
my %expected_downloaded_files = ( |
| 93 |
'somefile.txt' => { |
| 94 |
content => "blabla", |
| 95 |
}, |
| 96 |
); |
| 97 |
|
| 98 |
my $sslsock = SSLTest->new(cmdline => $cmdline, |
| 99 |
input => \%urls, |
| 100 |
errcode => $expected_error_code, |
| 101 |
existing => \%existing_files, |
| 102 |
certfile => $usrcrt, |
| 103 |
keyfile => $usrkey, |
| 104 |
lhostname => $testhostname, |
| 105 |
sslport => $port); |
| 106 |
if ($sslsock->run() == 0) |
| 107 |
{ |
| 108 |
exit 0; |
| 109 |
} |
| 110 |
|
| 111 |
# Retry the test with --no-check-certificate. expect success |
| 112 |
$port = 31443; |
| 113 |
$cmdline = $WgetTest::WGETPATH . " --ca-certificate=$srcdir/certs/wotca.pem". |
| 114 |
" https://$testhostname:$port/somefile.txt"; |
| 115 |
|
| 116 |
$expected_error_code = 0; |
| 117 |
|
| 118 |
my $retryssl = SSLTest->new(cmdline => $cmdline, |
| 119 |
input => \%urls, |
| 120 |
errcode => $expected_error_code, |
| 121 |
existing => \%existing_files, |
| 122 |
output => \%expected_downloaded_files, |
| 123 |
certfile => $usrcrt, |
| 124 |
keyfile => $usrkey, |
| 125 |
lhostname => $testhostname, |
| 126 |
sslport => $port); |
| 127 |
exit $retryssl->run(); |
| 128 |
# vim: et ts=4 sw=4 |