| 1 | package Mojo::IOLoop::TLS; | ||||||||||||||||||||||||||||
| 2 | 33 | ▶ | use Mojo::Base 'Mojo::EventEmitter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 33 | ▶ | use Mojo::File qw(curfile); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 33 | ▶ | use Mojo::IOLoop; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 33 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # TLS support requires IO::Socket::SSL | ||||||||||||||||||||||||||||
| 9 | 33 | ▶ | use constant TLS => $ENV{MOJO_NO_TLS} ? 0 : !!eval { require IO::Socket::SSL; IO::Socket::SSL->VERSION('2.009'); 1 }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 10 | 33 | ▶ | use constant READ => TLS ? IO::Socket::SSL::SSL_WANT_READ() : 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 33 | ▶ | use constant WRITE => TLS ? IO::Socket::SSL::SSL_WANT_WRITE() : 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | has reactor => sub { Mojo::IOLoop->singleton->reactor }, weak => 1; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # To regenerate the certificate run this command (28.06.2019) | ||||||||||||||||||||||||||||
| 16 | # openssl req -x509 -newkey rsa:4096 -nodes -sha256 -out server.crt \ | ||||||||||||||||||||||||||||
| 17 | # -keyout server.key -days 7300 -subj '/CN=localhost' | ||||||||||||||||||||||||||||
| 18 | 33 | my $CERT = curfile->sibling('resources', 'server.crt')->to_string; | |||||||||||||||||||||||||||
| 19 | 33 | my $KEY = curfile->sibling('resources', 'server.key')->to_string; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub DESTROY { shift->_cleanup } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 52 | ▶ | sub can_tls {TLS} | ||||||||||||||||||||||||||
|
Subroutine
can_tls: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub negotiate { | ||||||||||||||||||||||||||
|
Subroutine
negotiate: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my ($self, $args) = (shift, ref $_[0] ? $_[0] : {@_}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | return $self->emit(error => 'IO::Socket::SSL 2.009+ required for TLS support') unless TLS; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | my $handle = $self->{handle}; | |||||||||||||||||||||||||||
| 31 | return $self->emit(error => $IO::Socket::SSL::SSL_ERROR) | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | unless IO::Socket::SSL->start_SSL($handle, %{$self->_expand($args)}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | $self->reactor->io($handle => sub { $self->_tls($handle, $args->{server}) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub new { shift->SUPER::new(handle => shift) } | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub _cleanup { | ||||||||||||||||||||||||||
|
Subroutine
_cleanup: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 39 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | return undef unless my $reactor = $self->reactor; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $reactor->remove($self->{handle}) if $self->{handle}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | return $self; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub _expand { | ||||||||||||||||||||||||||
|
Subroutine
_expand: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||
| 46 | 0 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | weaken $self; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | my $tls = {SSL_error_trap => sub { $self->_cleanup->emit(error => $_[1]) }, SSL_startHandshake => 0}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $tls->{SSL_ca_file} = $args->{tls_ca} if $args->{tls_ca} && -T $args->{tls_ca}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"tls_ca"} and -T $args->{"tls_ca"}
MC/DC0%0/2
$args->{"tls_ca"} and -T $args->{"tls_ca"}
$args->{"tls_ca"}
-T $args->{"tls_ca"}
Truth table0%0/3
A and B
A= $args->{"tls_ca"}B= -T $args->{"tls_ca"}
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $tls->{SSL_cert_file} = $args->{tls_cert} if $args->{tls_cert}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $tls->{SSL_key_file} = $args->{tls_key} if $args->{tls_key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $tls->{SSL_server} = $args->{server} if $args->{server}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | @{$tls}{keys %{$args->{tls_options}}} = values %{$args->{tls_options}} if $args->{tls_options}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($args->{server}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | $tls->{SSL_cert_file} ||= $CERT; | ||||||||||||||||||||||||||
|
Condition0%0/3
$tls->{"SSL_cert_file"} ||= $CERT
MC/DC0%0/2
$tls->{"SSL_cert_file"} ||= $CERT
$tls->{"SSL_cert_file"}
$CERT
Truth table0%0/3
A ||= B
A= $tls->{"SSL_cert_file"}B= $CERT
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | $tls->{SSL_key_file} ||= $KEY; | ||||||||||||||||||||||||||
|
Condition0%0/3
$tls->{"SSL_key_file"} ||= $KEY
MC/DC0%0/2
$tls->{"SSL_key_file"} ||= $KEY
$tls->{"SSL_key_file"}
$KEY
Truth table0%0/3
A ||= B
A= $tls->{"SSL_key_file"}B= $KEY
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | unless (exists $tls->{SSL_hostname}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $tls->{SSL_hostname} = IO::Socket::SSL->can_client_sni ? $args->{address} : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $tls->{SSL_verifycn_name} ||= $args->{address}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$tls->{"SSL_verifycn_name"} ||= $args->{"address"}
MC/DC0%0/2
$tls->{"SSL_verifycn_name"} ||= $args->{"address"}
$tls->{"SSL_verifycn_name"}
$args->{"address"}
Truth table0%0/3
A ||= B
A= $tls->{"SSL_verifycn_name"}B= $args->{"address"}
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | return $tls; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub _tls { | ||||||||||||||||||||||||||
|
Subroutine
_tls: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 71 | 0 | my ($self, $handle, $server) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # Switch between reading and writing | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if (!($server ? $handle->accept_SSL : $handle->connect_SSL)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 75 | 0 | my $err = $IO::Socket::SSL::SSL_ERROR; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($err == READ) { $self->reactor->watch($handle, 1, 0) } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 77 | 0 | elsif ($err == WRITE) { $self->reactor->watch($handle, 1, 1) } | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | else { $self->_cleanup->emit(upgrade => delete $self->{handle}) } | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 33 | 1; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =encoding utf8 | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 NAME | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | Mojo::IOLoop::TLS - Non-blocking TLS handshake | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | use Mojo::IOLoop::TLS; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Negotiate TLS | ||||||||||||||||||||||||||||
| 96 | my $tls = Mojo::IOLoop::TLS->new($old_handle); | ||||||||||||||||||||||||||||
| 97 | $tls->on(upgrade => sub ($tls, $new_handle) {...}); | ||||||||||||||||||||||||||||
| 98 | $tls->on(error => sub ($tls, $err) {...}); | ||||||||||||||||||||||||||||
| 99 | $tls->negotiate(server => 1, tls_version => 'TLSv1_2'); | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Start reactor if necessary | ||||||||||||||||||||||||||||
| 102 | $tls->reactor->start unless $tls->reactor->is_running; | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | L<Mojo::IOLoop::TLS> negotiates TLS for L<Mojo::IOLoop>. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 EVENTS | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | L<Mojo::IOLoop::TLS> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 upgrade | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | $tls->on(upgrade => sub ($tls, $handle) {...}); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Emitted once TLS has been negotiated. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 error | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | $tls->on(error => sub ($tls, $err) {...}); | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Emitted if an error occurs during negotiation, fatal if unhandled. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | L<Mojo::IOLoop::TLS> implements the following attributes. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head2 reactor | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | my $reactor = $tls->reactor; | ||||||||||||||||||||||||||||
| 131 | $tls = $tls->reactor(Mojo::Reactor::Poll->new); | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Low-level event reactor, defaults to the C<reactor> attribute value of the global L<Mojo::IOLoop> singleton. Note that | ||||||||||||||||||||||||||||
| 134 | this attribute is weakened. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 METHODS | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | L<Mojo::IOLoop::TLS> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head2 can_tls | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | my $bool = Mojo::IOLoop::TLS->can_tls; | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | True if L<IO::Socket::SSL> 2.009+ is installed and TLS support enabled. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head2 negotiate | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | $tls->negotiate(server => 1, tls_version => 'TLSv1_2'); | ||||||||||||||||||||||||||||
| 149 | $tls->negotiate({server => 1, tls_version => 'TLSv1_2'}); | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Negotiate TLS. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | These options are currently available: | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =over 2 | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =item server | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | server => 1 | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Negotiate TLS from the server-side, defaults to the client-side. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =item tls_ca | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | tls_ca => '/etc/tls/ca.crt' | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Path to TLS certificate authority file. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =item tls_cert | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | tls_cert => '/etc/tls/server.crt' | ||||||||||||||||||||||||||||
| 172 | tls_cert => {'mojolicious.org' => '/etc/tls/mojo.crt'} | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Path to the TLS cert file, defaults to a built-in test certificate on the server-side. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =item tls_key | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | tls_key => '/etc/tls/server.key' | ||||||||||||||||||||||||||||
| 179 | tls_key => {'mojolicious.org' => '/etc/tls/mojo.key'} | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Path to the TLS key file, defaults to a built-in test key on the server-side. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =item tls_options | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | tls_options => {SSL_alpn_protocols => ['foo', 'bar'], SSL_verify_mode => 0x00, SSL_version => 'TLSv1_2'} | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Additional options for L<IO::Socket::SSL>. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =back | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head2 new | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | my $tls = Mojo::IOLoop::TLS->new($handle); | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Construct a new L<Mojo::IOLoop::Stream> object. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =cut | ||||||||||||||||||||||||||||