| 1 |
use lib 'inc'; |
| 2 |
|
| 3 |
use Net::SSLeay; |
| 4 |
use Test::Net::SSLeay qw(initialise_libssl); |
| 5 |
|
| 6 |
use IO::Socket::INET; |
| 7 |
|
| 8 |
if (!defined &Net::SSLeay::OCSP_response_status) { |
| 9 |
plan skip_all => 'No support for OCSP in your OpenSSL'; |
| 10 |
} |
| 11 |
|
| 12 |
#$Net::SSLeay::trace=3; |
| 13 |
|
| 14 |
my @tests = ( |
| 15 |
{ |
| 16 |
# this should give us OCSP stapling |
| 17 |
host => 'www.microsoft.com', |
| 18 |
port => 443, |
| 19 |
fingerprint => '5f0b37e633840ca02468552ea3b1197e5e118f7b', |
| 20 |
ocsp_staple => 1, |
| 21 |
expect_status => Net::SSLeay::V_OCSP_CERTSTATUS_GOOD(), |
| 22 |
}, |
| 23 |
{ |
| 24 |
# no OCSP stapling |
| 25 |
host => 'www.heise.de', |
| 26 |
port => 443, |
| 27 |
fingerprint => '36a7d7bfc59db65c040bccd291ae563d9ef7bafc', |
| 28 |
expect_status => Net::SSLeay::V_OCSP_CERTSTATUS_GOOD(), |
| 29 |
}, |
| 30 |
{ |
| 31 |
# this is revoked |
| 32 |
host => 'revoked.grc.com', |
| 33 |
port => 443, |
| 34 |
fingerprint => '310665f4c8e78db761c764e798dca66047341264', |
| 35 |
expect_status => Net::SSLeay::V_OCSP_CERTSTATUS_REVOKED(), |
| 36 |
}, |
| 37 |
); |
| 38 |
|
| 39 |
my $release_tests = $ENV{RELEASE_TESTING} ? 1:0; |
| 40 |
plan tests => $release_tests + @tests; |
| 41 |
|
| 42 |
initialise_libssl(); |
| 43 |
|
| 44 |
my $timeout = 10; # used to TCP connect and SSL connect |
| 45 |
my $http_ua = eval { require HTTP::Tiny } && HTTP::Tiny->new(verify_SSL => 0); |
| 46 |
|
| 47 |
my $sha1 = Net::SSLeay::EVP_get_digestbyname('sha1'); |
| 48 |
|
| 49 |
|
| 50 |
my @fp_mismatch; |
| 51 |
TEST: |
| 52 |
for my $test (@tests) { |
| 53 |
my $cleanup = __cleanup__->new; |
| 54 |
SKIP: { |
| 55 |
skip 'HTTP::Tiny required but not installed', 1 |
| 56 |
unless $http_ua; |
| 57 |
|
| 58 |
my $cl = IO::Socket::INET->new( |
| 59 |
PeerAddr => $test->{host}, |
| 60 |
PeerPort => $test->{port}, |
| 61 |
Timeout => $timeout, |
| 62 |
); |
| 63 |
skip "TCP connect to $test->{host}:$test->{port} failed: $!",1 |
| 64 |
if !$cl; |
| 65 |
diag("tcp connect to $test->{host}:$test->{port} ok"); |
| 66 |
|
| 67 |
my $ctx = Net::SSLeay::CTX_new() or die "failed to create CTX"; |
| 68 |
|
| 69 |
# enable verification with hopefully usable CAs |
| 70 |
Net::SSLeay::CTX_set_default_verify_paths($ctx); |
| 71 |
Net::SSLeay::CTX_load_verify_locations($ctx, |
| 72 |
Mozilla::CA::SSL_ca_file(),'') |
| 73 |
if eval { require Mozilla::CA }; |
| 74 |
Net::SSLeay::CTX_set_verify($ctx,Net::SSLeay::VERIFY_PEER(),undef); |
| 75 |
|
| 76 |
# setup TLS extension callback to catch stapled OCSP response |
| 77 |
my $stapled_response; |
| 78 |
Net::SSLeay::CTX_set_tlsext_status_cb($ctx,sub { |
| 79 |
my ($ssl,$resp) = @_; |
| 80 |
diag("got ".($resp ? '':'no ')."stapled OCSP response"); |
| 81 |
return 1 if ! $resp; |
| 82 |
$stapled_response = Net::SSLeay::i2d_OCSP_RESPONSE($resp); |
| 83 |
return 1; |
| 84 |
}); |
| 85 |
|
| 86 |
# create SSL object only after we have the context fully done since |
| 87 |
# some parts of the context (like verification mode) will be copied |
| 88 |
# to the SSL object and thus later changes to the CTX don't affect |
| 89 |
# the SSL object |
| 90 |
my $ssl = Net::SSLeay::new($ctx) or die "failed to create SSL"; |
| 91 |
|
| 92 |
# setup TLS extension to request stapled OCSP response |
| 93 |
Net::SSLeay::set_tlsext_status_type($ssl, |
| 94 |
Net::SSLeay::TLSEXT_STATUSTYPE_ocsp()); |
| 95 |
|
| 96 |
# non-blocking SSL_connect with timeout |
| 97 |
$cl->blocking(0); |
| 98 |
Net::SSLeay::set_fd($ssl,fileno($cl)); |
| 99 |
my $end = time() + $timeout; |
| 100 |
my ($rv,@err); |
| 101 |
while (($rv = Net::SSLeay::connect($ssl)) < 0) { |
| 102 |
my $to = $end-time(); |
| 103 |
$to<=0 and last; |
| 104 |
my $err = Net::SSLeay::get_error($ssl,$rv); |
| 105 |
vec( my $vec = '',fileno($cl),1) = 1; |
| 106 |
if ( $err == Net::SSLeay::ERROR_WANT_READ()) { |
| 107 |
select($vec,undef,undef,$to); |
| 108 |
} elsif ( $err == Net::SSLeay::ERROR_WANT_WRITE()) { |
| 109 |
select(undef,$vec,undef,$to); |
| 110 |
} else { |
| 111 |
while ( my $err = Net::SSLeay::ERR_get_error()) { |
| 112 |
push @err, Net::SSLeay::ERR_error_string($err); |
| 113 |
} |
| 114 |
last |
| 115 |
} |
| 116 |
} |
| 117 |
skip "SSL_connect with $test->{host}:$test->{port} failed: @err",1 |
| 118 |
if $rv<=0; |
| 119 |
diag("SSL_connect ok"); |
| 120 |
|
| 121 |
# make sure we talk to the right party, e.g. no SSL interception |
| 122 |
my $leaf_cert = Net::SSLeay::get_peer_certificate($ssl); |
| 123 |
$cleanup->add(sub { Net::SSLeay::X509_free($leaf_cert) }) if $leaf_cert; |
| 124 |
my $fp = $leaf_cert |
| 125 |
&& unpack("H*",Net::SSLeay::X509_digest($leaf_cert,$sha1)); |
| 126 |
skip "could not get fingerprint",1 if !$fp; |
| 127 |
if ($fp ne $test->{fingerprint}) { |
| 128 |
push @fp_mismatch, [ $fp,$test ]; |
| 129 |
skip("bad fingerprint for $test->{host}:$test->{port} -". |
| 130 |
" expected $test->{fingerprint}, got $fp",1) |
| 131 |
} |
| 132 |
diag("fingerprint matches"); |
| 133 |
|
| 134 |
if ( $test->{ocsp_staple} && ! $stapled_response ) { |
| 135 |
fail("did not get expected stapled OCSP response on $test->{host}:$test->{port}"); |
| 136 |
next TEST; |
| 137 |
} |
| 138 |
|
| 139 |
# create OCSP_REQUEST for all certs |
| 140 |
my @requests; |
| 141 |
for my $cert (Net::SSLeay::get_peer_cert_chain($ssl)) { |
| 142 |
my $subj = Net::SSLeay::X509_NAME_oneline( |
| 143 |
Net::SSLeay::X509_get_subject_name($cert)); |
| 144 |
my $uri = Net::SSLeay::P_X509_get_ocsp_uri($cert); |
| 145 |
if (!$uri) { |
| 146 |
diag("no OCSP URI for cert $subj"); |
| 147 |
next; |
| 148 |
} |
| 149 |
my $id = eval { Net::SSLeay::OCSP_cert2ids($ssl,$cert) } or do { |
| 150 |
fail("failed to get OCSP_CERTIDs for cert $subj: $@"); |
| 151 |
next TEST; |
| 152 |
}; |
| 153 |
my $req = Net::SSLeay::OCSP_ids2req($id); |
| 154 |
push @requests, [ $uri,$req,$id,$subj ]; |
| 155 |
$cleanup->add(sub { Net::SSLeay::OCSP_REQUEST_free($req) }); |
| 156 |
} |
| 157 |
if (!@requests) { |
| 158 |
fail("no certificate checks for $test->{host}:$test->{port}"); |
| 159 |
next TEST; |
| 160 |
} |
| 161 |
|
| 162 |
my $check_response = sub { |
| 163 |
my ($resp,$req,$id,$expect_status) = @_; |
| 164 |
if ( Net::SSLeay::OCSP_response_status($resp) |
| 165 |
!= Net::SSLeay::OCSP_RESPONSE_STATUS_SUCCESSFUL()) { |
| 166 |
return [ undef,"response bad status ". |
| 167 |
Net::SSLeay::OCSP_response_status_str(Net::SSLeay::OCSP_response_status($resp)) ]; |
| 168 |
} elsif ( ! eval { |
| 169 |
Net::SSLeay::OCSP_response_verify($ssl,$resp,$req) }) { |
| 170 |
return [ undef,"cannot verify response: $@" ]; |
| 171 |
} |
| 172 |
# extract result for id |
| 173 |
my ($status) = Net::SSLeay::OCSP_response_results($resp,$id); |
| 174 |
return [ undef,"no data for cert in response: $status->[1]" ] |
| 175 |
if ! $status->[2]; |
| 176 |
if ($expect_status != $status->[2]{statusType}) { |
| 177 |
return [ undef, |
| 178 |
"unexpected status=$status->[2]{statusType} (expected $expect_status): $status->[1]" ] |
| 179 |
} elsif ( $status->[2]{nextUpdate} ) { |
| 180 |
diag("status=$expect_status as expected: nextUpd=".localtime($status->[2]{nextUpdate})); |
| 181 |
} else { |
| 182 |
diag("status=$expect_status as expected: no nextUpd"); |
| 183 |
} |
| 184 |
return $status; |
| 185 |
}; |
| 186 |
|
| 187 |
if ($stapled_response) { |
| 188 |
my $stat = $check_response->( |
| 189 |
Net::SSLeay::d2i_OCSP_RESPONSE($stapled_response), |
| 190 |
undef, # no OCSP_REQUEST |
| 191 |
$requests[0][2], # stapled response is for the leaf certificate |
| 192 |
$test->{expect_status} |
| 193 |
); |
| 194 |
if (!$stat->[0]) { |
| 195 |
fail($stat->[1]); |
| 196 |
next TEST; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
for(my $i=0;$i<@requests;$i++) { |
| 201 |
my ($uri,$req,$id,$subj) = @{$requests[$i]}; |
| 202 |
if ( ! $http_ua ) { |
| 203 |
diag("no HTTP: skip checking $uri | $subj"); |
| 204 |
next |
| 205 |
} |
| 206 |
my $res = $http_ua->request('POST',$uri, { |
| 207 |
headers => { 'Content-type' => 'application/ocsp-request' }, |
| 208 |
content => Net::SSLeay::i2d_OCSP_REQUEST($req), |
| 209 |
timeout => $timeout, |
| 210 |
}); |
| 211 |
if (!$res->{success}) { |
| 212 |
if ($res->{status} == 599) { |
| 213 |
# internal error, assume network problem |
| 214 |
diag("disabling HTTP because of $http_ua->{reason}"); |
| 215 |
$http_ua = undef; |
| 216 |
} |
| 217 |
diag("$http_ua->{reason}: skip checking $uri | $subj"); |
| 218 |
next; |
| 219 |
} |
| 220 |
my $resp = eval { Net::SSLeay::d2i_OCSP_RESPONSE($res->{content}) }; |
| 221 |
if (!$resp) { |
| 222 |
diag("bad OCSP response($@): skip checking $uri | $subj"); |
| 223 |
next; |
| 224 |
} |
| 225 |
my $stat = $check_response->( |
| 226 |
$resp, |
| 227 |
$req, |
| 228 |
$id, |
| 229 |
($i>0) ? Net::SSLeay::V_OCSP_CERTSTATUS_GOOD() : $test->{expect_status}, |
| 230 |
); |
| 231 |
if (!$stat->[0]) { |
| 232 |
fail($stat->[1]); |
| 233 |
next TEST; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
pass("OCSP test $test->{host}:$test->{port} ok"); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
if ($release_tests) { |
| 242 |
if (!@fp_mismatch) { |
| 243 |
pass("all fingerprints matched"); |
| 244 |
} else { |
| 245 |
for(@fp_mismatch) { |
| 246 |
my ($fp,$test) = @$_; |
| 247 |
diag("fingerprint mismatch for $test->{host}:$test->{port} -". |
| 248 |
" expected $test->{fingerprint}, got $fp") |
| 249 |
} |
| 250 |
fail("some fingerprints did not matched - please adjust test"); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
{ |
| 255 |
# cleanup stuff when going out of scope |
| 256 |
package __cleanup__; |
| 257 |
sub new { bless [],shift }; |
| 258 |
sub add { my $self = shift; push @$self,@_ } |
| 259 |
sub DESTROY { |
| 260 |
my $self = shift; |
| 261 |
&$_ for(@$self) |
| 262 |
} |
| 263 |
} |