App-WebPerimeterScan
view release on metacpan or search on metacpan
lib/App/WebPerimeterScan/TLS.pm view on Meta::CPAN
$result->{verify_error} = $class->_verify($host, $port, $timeout);
} else {
$result->{error} = _ssl_error() // $class->i18n('tls.unknown_error');
}
# The version probes run even when the default handshake failed: a
# server that only speaks TLS 1.0 fails it, and that is exactly the
# server whose protocol list matters most.
for my $version (@PROBE_VERSIONS) {
my $probe = _connect($host, $port, $timeout, $version) or next;
push @{ $result->{protocols} }, $version;
close $probe;
}
return $result;
}
# _describe
# Purpose: record what the first handshake learned.
# Entry: $sock is a connected IO::Socket::SSL; $result a hashref.
# Exit: $result, filled in.
# Side effects: modifies $result.
sub _describe :Private
{
my ($sock, $result) = @_;
$result->{ok} = 1;
$result->{protocol} = $sock->get_sslversion // '';
$result->{cipher} = $sock->get_cipher // '';
$result->{self_signed} = _self_signed($sock);
# Each field is optional: some builds do not offer every one.
for my $field (qw(subject issuer fingerprint)) {
my $value = eval { $sock->peer_certificate($field) } or next;
$result->{$field} = $field eq 'fingerprint' ? $value : _name($value);
}
if(defined(my $not_after = _not_after($sock))) {
$result->{not_after} = $not_after;
if(defined(my $epoch = _parse_asn1_time($not_after))) {
$result->{expires_at} = $epoch;
$result->{days_left} = int(($epoch - time) / $SECONDS_PER_DAY);
}
}
return $result;
}
# _connect
# Purpose: one unverified handshake, optionally pinned to a version.
# Entry: $version, if given, is one of @PROBE_VERSIONS.
# Exit: the socket, or undef (SSL_ERROR / $! set).
# Side effects: a TLS handshake.
sub _connect :Private
{
my ($host, $port, $timeout, $version) = @_;
my %args = (
PeerHost => $host,
PeerPort => $port,
Timeout => $timeout,
SSL_hostname => $host,
SSL_verify_mode => SSL_VERIFY_NONE,
);
$args{SSL_version} = $version if $version;
$args{SSL_cipher_list} = $PROBE_CIPHERS{$version} if $version && $PROBE_CIPHERS{$version};
return IO::Socket::SSL->new(%args);
}
# _verify
# Purpose: would a browser trust this chain for this name?
# Entry: as _connect.
# Exit: undef if it verifies, else the reason.
# Side effects: a TLS handshake.
sub _verify :Private
{
my ($class, $host, $port, $timeout) = @_;
my $sock = IO::Socket::SSL->new(
PeerHost => $host,
PeerPort => $port,
Timeout => $timeout,
SSL_hostname => $host,
SSL_verify_mode => SSL_VERIFY_PEER,
SSL_verifycn_name => $host,
SSL_verifycn_scheme => 'http',
);
close $sock if $sock;
return $sock ? undef : (_ssl_error() // $class->i18n('tls.verify_failed'));
}
# _ssl_error
# Purpose: IO::Socket::SSL's last error, or $! when it has none.
# Entry: straight after a failed IO::Socket::SSL->new.
# Exit: a non-empty string, or undef if neither says anything.
# Side effects: none.
sub _ssl_error :Private
{
my $error = $IO::Socket::SSL::SSL_ERROR;
$error = "$!" unless defined $error && length $error;
return length $error ? $error : undef;
}
# _self_signed
# Purpose: are the certificate's subject and issuer the same name?
# Entry: $sock is a connected IO::Socket::SSL.
# Exit: 1 or 0; 0 when either name cannot be read.
# Side effects: none.
sub _self_signed :Private
{
my ($sock) = @_;
my $subject = eval { $sock->peer_certificate('subject') };
my $issuer = eval { $sock->peer_certificate('issuer') };
return ($subject && $issuer && _name($subject) eq _name($issuer)) ? 1 : 0;
}
# _name
# Purpose: a certificate name as one string.
# Entry: $name is a string, a hash of fields, or undef.
# Exit: the name; '' for undef; 'unknown' for a hash with no fields
# we know.
# Side effects: none.
#
# Some IO::Socket::SSL builds return the whole name as one string, others as
# a hash of fields whose values are array references. Both shapes are
# accepted: reporting an empty name for one of them made subject and issuer
# compare equal, which turned every certificate into a self-signed one.
sub _name :Private
{
my ($name) = @_;
return '' unless defined $name;
return $name unless ref $name;
return '' unless ref $name eq 'HASH';
my @parts = grep { defined && !ref && length }
map { ref eq 'ARRAY' ? $_->[0] : $_ }
grep { defined } @{$name}{@NAME_FIELDS};
( run in 1.667 second using v1.01-cache-2.11-cpan-234fa220139 )